-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
70 lines (39 loc) · 1.22 KB
/
app.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
65
66
67
68
69
70
import socket
from Memcache import Memcache
from main import *
from Memcache import *
from _thread import *
#instatiate cache
memcache = Memcache()
threadLock = threading.Lock()
def Main():
HOST = '127.0.0.1' # Standard loopback interface address (localhost)
PORT = 5000 # Port to listen on
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
#5 is suggested num of non-accepted outstanding connections
s.listen(5)
while(True):
conn, addr = s.accept()
threadLock.acquire()
print('Connected by', addr, flush=True)
start_new_thread(threaded,(conn,))
#not reachable
s.close()
def threaded(conn):
while(True):
data = conn.recv(1024)
print("Incoming: ", data, flush=True)
if not data:
print("End of stream...", flush=True)
#release lock
threadLock.release()
break
try:
response = memcached_process(memcache, data)
except UnsupportedOperation:
response = str_to_bytes("Unsupported op")
conn.send(response)
conn.close()
if __name__ == '__main__':
Main()