Skip to content

Instantly share code, notes, and snippets.

@satishgoda
Last active Sep 19, 2017
Embed
What would you like to do?
Web Programming
/foo/bar?yeah=loo
你好
from http.server import BaseHTTPRequestHandler
from urllib import parse
import cgi
import io
class GetPostHandler(BaseHTTPRequestHandler):
def do_POST(self):
# Parse the form data posted
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ = {
'REQUEST_METHOD': 'POST',
'CONTENT_TYPE': self.headers['Content-Type'],
}
)
self.send_response(200)
self.send_header('Content-Type',
'text/plain; charset=utf-8' )
self.end_headers()
out = io.TextIOWrapper(
self.wfile,
encoding='utf-8',
line_buffering=False,
write_through=True,
)
for field in form.keys():
item = form[field]
out.write("\t{}={}\n".format(field, item.value))
out.detach()
def do_GET(self):
self.send_response(200)
self.send_header('Content-Type',
'text/plain; charset=utf-8')
self.end_headers()
self.wfile.write(self.path.encode())
self.wfile.write("\n".encode())
self.wfile.write("你好".encode())
if __name__ == '__main__':
from http.server import HTTPServer
server = HTTPServer(('localhost', 8899), GetPostHandler)
print('Starting server, use <Ctrl-C> to stop')
server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment