Skip to content

Instantly share code, notes, and snippets.

@chuwy
Last active April 4, 2016 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chuwy/5ef911ba1e459cbbfdcd8fa94942a81e to your computer and use it in GitHub Desktop.
Save chuwy/5ef911ba1e459cbbfdcd8fa94942a81e to your computer and use it in GitHub Desktop.
Webserver printing headers
#!/usr/bin/env python
"""
Webserver printing headers
"""
import SimpleHTTPServer
import SocketServer
import logging
import sys
count = 0
class GetHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
def do_POST(self):
logging.error(self.headers)
SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET(self)
def do_GET(self):
global count
print(count)
count = count + 1
if count > 5:
logging.error(self.headers)
self.send_response(200, None)
self.send_header('Content-type', 'text/plain')
self.send_header('Accept-Ranges', 'bytes')
self.send_header('Connection', 'close')
self.end_headers()
self.wfile.write(bytearray("bye", 'utf-8'))
else:
logging.error(self.headers)
self.send_response(404, None)
self.send_header('Content-type', 'text/plain')
self.send_header('Accept-Ranges', 'bytes')
self.send_header('Connection', 'close')
self.end_headers()
self.wfile.write(bytearray("fuck", 'utf-8'))
try:
port = int(sys.argv[1])
except:
port = 8082
Handler = GetHandler
httpd = SocketServer.TCPServer(("0.0.0.0", port), Handler)
httpd.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment