File tree Expand file tree Collapse file tree 1 file changed +35
-0
lines changed
Chapter2/Work4:HttpWebProxyServer Expand file tree Collapse file tree 1 file changed +35
-0
lines changed Original file line number Diff line number Diff line change 1+ from socket import *
2+
3+ serverSocket = socket (AF_INET , SOCK_STREAM )
4+ serverSocket .bind (('' , 10086 ))
5+ serverSocket .listen (2 )
6+
7+ while True :
8+ print ('ready to serve...' )
9+ connectionSocket , address = serverSocket .accept ()
10+ message = connectionSocket .recv (4096 ).decode ()
11+ print ('request:\n ' + message )
12+ filename = message .split ()[1 ].partition ('/' )[2 ]
13+ print ('filename:' + filename )
14+ try :
15+ f = open (filename )
16+ outputdata = f .read ()
17+ connectionSocket .sendall (outputdata .encode ())
18+ except IOError :
19+ clientSocket = socket (AF_INET , SOCK_STREAM )
20+ clientSocket .connect ((filename .partition ('/' )[0 ], 80 ))
21+ request = message .replace (message .split ()[1 ], '/' + filename .partition ('/' )[2 ])
22+ print (request )
23+ request = message .replace ('127.0.0.1:10086' , filename .partition ('/' )[0 ])
24+ print (request )
25+ clientSocket .sendall (request .encode ())
26+ recv = clientSocket .recv (4096 )
27+ file = open ('./' + filename , 'w' )
28+ file .write (recv .decode ())
29+ connectionSocket .sendall (recv )
30+ clientSocket .close ()
31+ file .close ();
32+ connectionSocket .close ()
33+
34+
35+
You can’t perform that action at this time.
0 commit comments