Skip to content

Instantly share code, notes, and snippets.

@ismailakkila
Created December 21, 2017 15:41
Show Gist options
  • Save ismailakkila/6aab48476a5f55107d8f08f51da6f412 to your computer and use it in GitHub Desktop.
Save ismailakkila/6aab48476a5f55107d8f08f51da6f412 to your computer and use it in GitHub Desktop.
ch9_simplehttpserver.py
#ch9_simplehttpserver.py
import SimpleHTTPServer
import SocketServer
import urllib
#HTTP Request Handler Class
class RequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
#POST Method
def do_POST(self):
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length).decode("utf-8")
print(post_data)
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
webpage = "<html><head><h1>Success!</h1></head><body></body></html>"
self.wfile.write(webpage)
#GET Method
def do_GET(self):
if self.path == "/":
self.send_response(200)
self.send_header("Content-Type", "text/html")
self.end_headers()
webpage = "<html><head><h1>Hello World</h1></head><body></body></html>"
self.wfile.write(webpage)
#Start the Web Server on TCP 8080
web_server = SocketServer.TCPServer(("0.0.0.0", 8080), RequestHandler)
web_server.serve_forever()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment