-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathserver_proxy_cache_select.py
54 lines (42 loc) · 1.21 KB
/
server_proxy_cache_select.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
import select
import socket
import sys
import urllib2
HOST = ''
PORT = 8888
BACKLOG = 5
SIZE = 1024
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((HOST, PORT))
server.listen(BACKLOG)
input = [server, sys.stdin]
running = True
cache = {}
def get_webpage(url):
result = urllib2.urlopen(url).read()
cache[url] = result
return result
while running: # simple select reactor
inputready, outputready, exceptready = select.select(input, [], [])
for s in inputready:
if s == server:
client, address = server.accept()
input.append(client)
elif s == sys.stdin:
junk = sys.stdin.readline()
running = False
else:
data = s.recv(SIZE)
if data and data.startswith("http://"):
url = data.strip()
if url in cache:
print "fetched from cache"
data_to_client = cache[url]
else:
print "going to the interweb"
data_to_client = get_webpage(url)
s.send(data_to_client)
else:
s.close()
input.remove(s)
server.close()