Update server.py

add Content-Type to avoid quit
This commit is contained in:
Dylan Wu 2022-05-05 05:25:47 -04:00
parent 705f6ced68
commit 3014ebf00d
1 changed files with 19 additions and 6 deletions

View File

@ -4,6 +4,15 @@ HOST = ''
PORT = 8000
content = b'''HTTP/1.x 200 OK
Content-Type: text/html
<head>
<meta http-equiv="refresh" content="1; url=http://chat.localhost.observer/fax/">
<title>WOW</title>
</head>
<html>
<p>Wow, Python Server</p>
</html>
'''
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
@ -14,24 +23,28 @@ while True:
conn, addr = s.accept()
request = conn.recv(4096)
method = request.decode().split(' ')[0]
ctype = request.decode().split('Content-Type: ')[1].split('\r\n')[0]
# src = request.decode().split(' ')[1]
body = request.decode('utf-8').split('\r\n\r\n', 1)[1]
if method == 'POST':
single = str(body.encode('utf-8')).split('&',1)
sender = single[0].split('=',1)
print (request)
if method == 'POST' and ctype == 'application/x-www-form-urlencoded':
message = single[1].split('=',1)
with open('/dev/usb/lp0', 'w') as printer:
# printer.write('\n'+str(addr)+'\n')
# printer.write(str(request.decode('utf-8'))+'\n')
printer.write('\n'+str(addr)+'\n')
printer.write(str(request.decode('utf-8'))+'\n')
# printer.write('\n'*2+str(body)+'\n')
# printer.write(single[0]+'\n')
# printer.write(single[1]+'\n')
# printer.write(sender[0]+'\n')
printer.write(sender[1]+'\n')
printer.write(message[1]+'\n')
printer.write(message[1][:-1]+'\n')
print (sender)
print (message)
print (ctype)
conn.sendall(content)
conn.close()