-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
64 lines (47 loc) · 1.68 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#this code taken from: https://ruslanspivak.com/lsbaws-part1/
#sets up a very basic server that waits for GET requests
#and responds with the text "Hello, World!"
#you will probably have to download the mysql python connector: https://dev.mysql.com/doc/connector-python/en/connector-python-obtaining.html
#cannot get the database to work so might just work with pandas
#could work with sqlite since that's already available, but not sure
#how to make a db from a csv using sqlite
import socket
# import pandas
# import mysql.connector
# import peewee
# from peewee import *
# db = MySQLDatabase("johnnydb", user="john", passwd="megajohny")
HOST, PORT = "", 8888
listen_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
listen_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
listen_socket.bind((HOST, PORT))
listen_socket.listen(1)
print "Serving HTTP on port %s ..." % PORT
# create an internal pandas "database"
# database = pandas.read_csv("CCES_clean.csv")
while True:
client_connection, client_address = listen_socket.accept()
request = client_connection.recv(1024)
print request
if request[:15] == "GET /index.html":
html = open("index.html")
html_data = html.read()
# print html_data
html.close()
http_response = """\
HTTP/1.1 200 OK
"""
http_response += html_data
print http_response
else:
http_response = """\
HTTP/1.1 200 OK
"""
img = open("person.svg")
img_data = img.read()
# print html_data
img.close()
http_response += img_data
print http_response
client_connection.sendall(http_response)
client_connection.close()