-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
38 lines (34 loc) · 1.3 KB
/
server.py
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
import BaseHTTPServer
import os
import socket
class WebRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
def do_GET(self):
if self.path != "images/avatar.jpg":
hostname = socket.gethostname()
message_parts = [
"<html>",
"<head><title>Hello World `</title></head>",
"<body style=\"text-align:center;\">",
"<p><img src='images/avatar.jpg' /></p>",
"<h3>Hello Alauda!</h3><br />",
"<p>My hostname is <b>" + hostname + "</b></p>",
"</body>",
"<html>"
]
message = '\r\n'.join(message_parts)
print message
# message = "New request arrived from %s:%d" % self.client_address
self.send_response(200)
self.end_headers()
self.wfile.write(message)
else:
f = open(os.path.join(os.curdir, self.path))
self.send_response(200)
self.send_header('Content-type', 'image/jpg')
self.end_headers()
self.wfile.write(f.read())
f.close()
if __name__ == '__main__':
print "Server started, Listening on port 80"
server = BaseHTTPServer.HTTPServer(('0.0.0.0', 80), WebRequestHandler)
server.serve_forever()