RasppleII / a2cloud

Connect your Apple // to the world via Linux
Other
5 stars 2 forks source link

Contribution: Web Front-end for vsd #31

Open oliverschmidt opened 8 years ago

oliverschmidt commented 8 years ago

The vsd script is a great addition to ADTPro. However opening a Telnet session might not be the most straight forward thing to do in every scenario. Therefore I created a simplistic web front-end for vsd. I see two primary usage scenarios:

The web front-end makes use of the web server capabilities built into the standard Python libraries. So it doesn't depend on any additional installation:

#!/usr/bin/python
import BaseHTTPServer, subprocess, urlparse

class Handler(BaseHTTPServer.BaseHTTPRequestHandler):
    def do_GET(self):
        url = urlparse.urlsplit(self.path)
        if url.path != '/':
            self.send_error(404)
            return
        if url.query:
            query = dict(urlparse.parse_qsl(url.query))
            if 'vsd1' in query:
                subprocess.call(['vsd', '-1', query['vsd1']])
            if 'vsd2' in query:
                subprocess.call(['vsd', '-2', query['vsd2']])
        try:
            vsd1 = subprocess.check_output(['vsd', '-1'])[17:-1]
            vsd2 = subprocess.check_output(['vsd', '-2'])[17:-1]
        except subprocess.CalledProcessError:
            vsd1 = ""
            vsd2 = ""
        self.send_response(200)
        self.send_header('Content-type', 'text/html')
        self.end_headers()
        self.wfile.write('<html>\n')
        self.wfile.write('  <head>\n')
        self.wfile.write('    <title>A2CLOUD</title>\n')
        self.wfile.write('    <meta name="viewport" content="width=device-width, initial-scale=1">\n')
        self.wfile.write('  </head>\n')
        self.wfile.write('  <body style="font-family:sans-serif;">\n')
        self.wfile.write('    <h2>A2CLOUD</h2>\n')
        self.wfile.write('    <form action="/"><p>Drive 1\n')
        self.wfile.write('      <input type="text" name="vsd1" value="' + vsd1 + '" size="60" maxlength="120">\n')
        self.wfile.write('      <input type="submit" name="s" value="Mount">\n')
        self.wfile.write('    </form>\n')
        self.wfile.write('    <form action="/"><p>Drive 2\n')
        self.wfile.write('      <input type="text" name="vsd2" value="' + vsd2 + '" size="60" maxlength="120">\n')
        self.wfile.write('      <input type="submit" name="s" value="Mount">\n')
        self.wfile.write('    </form>\n')
        self.wfile.write('  </body>\n')
        self.wfile.write('</html>')

httpd = BaseHTTPServer.HTTPServer(('', 80), Handler)
httpd.serve_forever()

Beside using the HTML form it is as well possible to mount a disk image by specifying it right in the URL like http://<ip addr of Raspple II/?vsd1=<disk image name to mount in drive 1> which allows for further scenarios.

The web front-end can be started together with the ADTPro server by saving it as /usr/local/bin/webvsd.py, making it executable and adding it to adtpro-start right after the call to adtpro.sh as sudo nohup webvsd.py &> /dev/null &.

screenshot

oliverschmidt commented 8 years ago

I think it would be cleaner to replace

            if 'vsd1' in query:
                subprocess.call(['vsd', '-1', query['vsd1']])
            if 'vsd2' in query:
                subprocess.call(['vsd', '-2', query['vsd2']])

with

            if 'vsd1' in query:
                subprocess.call(['vsd', '-1', '-f', query['vsd1']])
            if 'vsd2' in query:
                subprocess.call(['vsd', '-2', '-f', query['vsd2']])

in order to avoid reading from stdin in the first place instead of relying on Python to make it work ;-)