Porxy Server
- source code
# proxyServer.py
from socket import *
tcpSerSock = socket(AF_INET, SOCK_STREAM)
server_port = 22500
tcpSerSock.bind(('', server_port))
tcpSerSock.listen(1)
while True:
print('Ready to serve...')
tcpCliSock, addr = tcpSerSock.accept()
print('Received a connection from:', addr)
message = tcpCliSock.recv(1024).decode(encoding="utf-8")
print(message)
# Extract the filename from the given message
filename = message.split()[1].partition("//")[2].replace('/', '_')
print(filename)
fileExist = "false"
try:
# Check whether the file exist in the cache
f = open(filename, "r")
outputdata = f.readlines()
outputdata = outputdata[outputdata.index("<html>\n")-1:]
fileExist = "true"
# ProxyServer finds a cache hit and generates a response message
tcpCliSock.send("HTTP/1.1 200 OK\r\n".encode(encoding="utf-8"))
tcpCliSock.send("Content-Type:text/html\r\n".encode(encoding="utf-8"))
print(outputdata)
for line in outputdata:
tcpCliSock.send(line.encode(encoding="utf-8"))
print('Read from cache')
# Error handling for file not found in cache
except IOError:
if fileExist == "false":
# Create a socket on the proxy server
c = socket(AF_INET, SOCK_STREAM)
hostname = message.split()[1].partition("//")[2].partition("/")[0].replace("www.", "", 1)
print("Host name:", hostname)
try:
# Connect to the socket to port 80
c.connect((hostname, 80))
c.send(message.encode(encoding="utf-8"))
buff = c.recv(1024).decode(encoding="utf-8")
# 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
tcpCliSock.send(buff.encode(encoding="utf-8"))
tmpFile = open("./" + filename, "w")
tmpFile.writelines(buff)
tmpFile.close()
except:
print("Illegal request")
else:
# HTTP response message for file not found
tcpCliSock.send("404 Found".encode(encoding="utf-8"))
tcpCliSock.close()
tcpSerSock.close()
-
Search Enter the proxy server, open proxy server settings set by the system, the address is set
http://localhost
, the port is set to programserver_port
-
Access http://gaia.cs.umass.edu/wireshark-labs/INTRO-wireshark-file1.html, requested html objects through a proxy server
because the proxy server is not cached, so the proxy server to server gaia.cs.umass.edu requesting the object in FigureHost name: gaia.cs.umass.edu
, the presence of the proxy server and the file object folder, the following results
-
Visit the page again, the request object through a proxy server
Because the proxy server has a cache of the object, the object thus taken directly from the cache to the browser, in FigureRead from cache
![proxy5](proxy5.png)// 代理服务器缓存的文件,除html外还包括响应报文的状态行和首部行
HTTP/1.1 200 OK
Date: Fri, 06 Mar 2020 07:35:34 GMT
Server: Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/5.4.16 mod_perl/2.0.11 Perl/v5.16.3
Last-Modified: Fri, 06 Mar 2020 06:59:02 GMT
ETag: "51-5a02a3045f9f3"
Accept-Ranges: bytes
Content-Length: 81
Content-Type: text/html; charset=UTF-8
<html>
Congratulations! You've downloaded the first Wireshark lab file!
</html>
It is worth mentioning that, in addition to the file containing the response message entity body, i.e. the outer part of html, further status line, the first line portion and the like, if the direct display appears
The process can be certain that object and then sent to the browser, to ensure that only displays useful information, or will be handled when you save the file object
outputdata = f.readlines()
outputdata = outputdata[outputdata.index("<html>\n")-1:]
-
Other Considerations
Proxy server is running at the time of this machine, if the IP address is set
127.0.0.1
, it may not function properlySolution: direct input null character, use the default value localhost
# tcpSerSock.bind(('127.0.0.1', server_port)) tcpSerSock.bind(('', server_port))