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