You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.0 KiB

8 years ago
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
import socket
import cgi
import json
import time
import os.path
import threading
TELNET_HOST = 'localhost'
TELNET_PORT = 4243
DOCROOT = '/var/tmp/blup2/blayer-webif'
UPLOAD_DIR = '/var/tmp/blup2/blm/incoming'
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((TELNET_HOST, TELNET_PORT))
socklock = threading.Lock()
class HTTPRequestHandler(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
print self.path
#self.wfile.write('<form action="bla" method="post"><input type="text" name="text" /><input type="submit" value="ok" /></form>')
p = os.path.join(DOCROOT, self.path[1:])
print p
if os.path.isfile(p):
f = open(p, 'r')
data = f.read()
f.close()
self.wfile.write(data)
def do_POST(self):
if self.path == '/do_upload':
formdata = cgi.FieldStorage(
fp=self.rfile, headers=self.headers,
environ={'REQUEST_METHOD': 'POST', 'CONTENT_TYPE': self.headers['Content-Type']})
fileField = formdata['file']
f = open(os.path.join(UPLOAD_DIR, fileField.filename), 'w')
f.write(fileField.file.read())
f.close()
sock.send(json.dumps({"cmd": "refresh"}) + '\n')
print 'server answer to refresh cmd: ', sock.recv(1024)
self.send_response(200);
self.end_headers();
return
postdata = self.rfile.read(int(self.headers.getheader('content-length')))
#self.wfile.write('test')
if postdata[-1:] != '\n':
postdata += '\n'
print 'postdata:', postdata
with socklock:
sock.send(postdata)
time.sleep(0.1)
resp = sock.recv(1024 * 10)
#sock.shutdown(socket.SHUT_RDWR)
#sock.close()
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.send_header('Content-length', len(resp))
self.end_headers()
print 'resp:', resp
self.wfile.write(resp)
server = HTTPServer(('0.0.0.0',8080), HTTPRequestHandler)
server.serve_forever()