forked from bigsnarfdude/pythonNetworkProgrammingN00B
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver_proxy_select.py
More file actions
46 lines (36 loc) · 984 Bytes
/
server_proxy_select.py
File metadata and controls
46 lines (36 loc) · 984 Bytes
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
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 = 1
def get_webpage(url):
result = urllib2.urlopen(url).read()
return result
while running:
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 = 0
else:
data = s.recv(size)
if data:
if "http://" in data:
url = data.strip()
data_to_client = get_webpage(url)
s.send(data_to_client)
else:
s.close()
input.remove(s)
server.close()