Created
December 21, 2017 15:41
-
-
Save ismailakkila/6aab48476a5f55107d8f08f51da6f412 to your computer and use it in GitHub Desktop.
ch9_simplehttpserver.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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