-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebProxy.py
70 lines (59 loc) · 2.31 KB
/
WebProxy.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
#coding:utf-8
from socket import *
# 创建socket,绑定到端口,开始监听
tcpSerPort = 8899
tcpSerSock = socket(AF_INET, SOCK_STREAM)
# Prepare a server socket
tcpSerSock.bind(('', tcpSerPort))
tcpSerSock.listen(5)
while True:
# 开始从客户端接收请求
print('Ready to serve...')
tcpCliSock, addr = tcpSerSock.accept()
print('Received a connection from: ', addr)
message = tcpCliSock.recv(4096).decode()
# 从请求中解析出filename
filename = message.split()[1].partition("//")[2].replace('/', '_')
fileExist = "false"
try:
# 检查缓存中是否存在该文件
f = open(filename, "r")
outputdata = f.readlines()
fileExist = "true"
print('File Exists!')
# 缓存中存在该文件,把它向客户端发送
for i in range(0, len(outputdata)):
tcpCliSock.send(outputdata[i].encode())
print('Read from cache')
# 缓存中不存在该文件,异常处理
except IOError:
print('File Exist: ', fileExist)
if fileExist == "false":
# 在代理服务器上创建一个tcp socket
print('Creating socket on proxyserver')
c = socket(AF_INET, SOCK_STREAM)
hostn = message.split()[1].partition("//")[2].partition("/")[0]
print('Host Name: ', hostn)
try:
# 连接到远程服务器80端口
c.connect((hostn, 80))
print('Socket connected to port 80 of the host')
c.sendall(message.encode())
# Read the response into buffer
buff = c.recv(4096)
tcpCliSock.sendall(buff)
# Create a new file in the cache for the requested file.
# Also send the response in the buffer to client socket
# and the corresponding file in the cache
tmpFile = open("./" + filename, "w")
tmpFile.writelines(buff.decode().replace('\r\n', '\n'))
tmpFile.close()
except:
print("Illegal request")
else:
# HTTP response message for file not found
# Do stuff here
print('File Not Found...Stupid Andy')
# Close the client and the server sockets
tcpCliSock.close()
tcpSerSock.close()