-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver
executable file
·43 lines (30 loc) · 1.29 KB
/
server
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
#!/usr/bin/python2.7 -tt
import socket
import sys
ERROR_MSG = "HTTP/1.1 404 Not Found\r\nContent-type: text/html\r\nContent-length: 113\r\n\r\n<html><head><title>Not Found</title></head><body>\r\nSorry, the object you requested was not found.\r\n</body></html>\r\n\r\n"""
def server_program():
CHUNK_SIZE = 1024
counter = 0
server_request_counter = 0
host = "127.0.0.1"
port = int(sys.argv[1])
while True:
server_socket = socket.socket()
server_socket.connect((host, port))
data = ''
while '\r\n\r\n' not in data:
data += server_socket.recv(CHUNK_SIZE).decode()
server_request_counter+= 1
print "Server got a request (" + str(server_request_counter) + ")..."
data_array = data.split(" ") # [GET, /counter, ...]
if(data_array[1] == "/counter"):
counter += 1
result = "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\nContent-Length: " + str(len(str(counter))) +"\r\n\r\n"+str(counter)+"\r\n\r\n"
else:
result = ERROR_MSG
print "returning the result..."
result = result.encode()
server_socket.send(result) # send result to the client
server_socket.close()
if __name__ == '__main__':
server_program()