forked from james-see/python-examples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver-example.py
More file actions
46 lines (38 loc) · 1.56 KB
/
server-example.py
File metadata and controls
46 lines (38 loc) · 1.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#!/usr/bin/python
# Author: James Campbell
# Date: May 23rd 2016
# Date Updated: June 8th 2016
# What: Starts a http server as an example (works in Python 3)
import time
from http.server import BaseHTTPRequestHandler, HTTPServer
# default unless set at prompt
hostPort = 10010
customPort = input("Default port 10010, hit enter or type custom one now: ")
if customPort != '':
hostPort = int(customPort)
hostName = "localhost"
class MyServer(BaseHTTPRequestHandler):
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(
bytes("<html><head><title>MY PYTHON WEB SERVER PAGE!</title></head>", "utf-8"))
self.wfile.write(
bytes("<body style='font-family:monospace;'><p>This is a test.</p>", "utf-8"))
# you can use if else to check path and do custom things based on path accessed
if self.path == '/win':
self.wfile.write(bytes("<p>YOU WIN! @ path %s</p>" % self.path, "utf-8"))
else:
self.wfile.write(bytes("<p>You accessed path: %s</p>" % self.path, "utf-8"))
self.wfile.write(bytes("</body></html>", "utf-8"))
myServer = HTTPServer((hostName, hostPort), MyServer)
print(time.asctime(), "Server Starts - %s:%s" % (hostName, hostPort))
# continue to serve until a keypress in terminal
try:
myServer.serve_forever()
except KeyboardInterrupt:
pass
myServer.server_close()
print(time.asctime(), "Server Stops - %s:%s" % (hostName, hostPort))
# python3 -m http.server 10010 --bind 127.0.0.1