Skip to content

Instantly share code, notes, and snippets.

@rinchik
Last active June 12, 2016 16:11
Show Gist options
  • Save rinchik/0f2f7e798998a0abf31df41ef87c2755 to your computer and use it in GitHub Desktop.
Save rinchik/0f2f7e798998a0abf31df41ef87c2755 to your computer and use it in GitHub Desktop.
Small Python web server that supports get and post
from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
from urlparse import parse_qs
PASSWORD = "abba"
class Handler(BaseHTTPRequestHandler):
def _set_headers(self):
self.send_response(200)
self.send_header('Content-type', 'text/html')
self.end_headers()
def do_GET(self):
self._set_headers()
self.send_form()
def do_POST(self):
length = int(self.headers['content-length'])
postvars = parse_qs(self.rfile.read(length), keep_blank_values=1)
password = postvars['password'][0]
self._set_headers()
print password
if password == PASSWORD:
self.send_success()
else:
self.send_form('Wrong pass!')
def send_form(self, wrong_password = ''):
self.wfile.write("<html><body><h1>Form:</h1>"+wrong_password+"<form action='/' method='POST'><input type='password' name='password'><input type='submit'></form></body></html>")
def send_success(self):
self.wfile.write("<html><body><h1>Our data is yours now!</h1></body></html>")
server_address = ('', 3333)
httpd = HTTPServer(server_address, Handler)
print 'Starting httpd on', server_address
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment