Python wrapper for running instances of trisurf-ng
Samo Penic
2017-01-27 20db829c3a764ae1ea8ff79412bc7e0e9a6c92a9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import subprocess
import sys, os
if sys.version_info>=(3,0):
    from urllib.parse import urlparse
else:
    from urlparse import urlparse
import http.server
import socketserver
 
#Web server
class TsWEB(http.server.BaseHTTPRequestHandler):
    def do_GET(self):
        parsed_path=urlparse(self.path)
        message_parts = [
                'CLIENT VALUES:',
                'client_address=%s (%s)' % (self.client_address, self.address_string()),
                'command=%s' % self.command,
                'path=%s' % self.path,
                'real path=%s' % parsed_path.path,
                'query=%s' % parsed_path.query,
                'request_version=%s' % self.request_version,
                '',
                'SERVER VALUES:',
                'server_version=%s' % self.server_version,
                'sys_version=%s' % self.sys_version,
                'protocol_version=%s' % self.protocol_version,
                '',
                'HEADERS RECEIVED:',
                ]
        for name, value in sorted(self.headers.items()):
            message_parts.append('%s=%s' % (name, value.rstrip()))
        message_parts.append('')
        message = '<br>'.join(message_parts)
        self.send_response(200)
        self.end_headers()
        self.wfile.write(b"<h1>Trisurf-ng manager web interface</h1><hr>")
        oldstdout=sys.stdout
        if(parsed_path.path=='/analysis'):
            process=subprocess.Popen (['/usr/bin/python3', sys.argv[0], '--analysis', parsed_path.query, '--html'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        else:
            process=subprocess.Popen (['/usr/bin/python3', sys.argv[0], '-s', '--html'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        stdout, stderr= process.communicate()
        output=stdout.decode('ascii')
        output=output+message
        output=output.replace('\n','<BR>')
        output=bytearray(output,'ascii')
        self.wfile.write(output)
 
 
 
 
class WebServer():
    def __init__(self, port=8000):
        http_server = socketserver.TCPServer(('', port), TsWEB)
        try:
            http_server.serve_forever()
        except KeyboardInterrupt:
            print('^C received, shutting down the web server')
            http_server.socket.close()