Porxyサーバー
- ソースコード
# 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()
-
プロキシサーバーを入力し検索すると、システムによって設定されたオープンプロキシサーバーの設定は、アドレスが設定され
http://localhost
たポートは、プログラムに設定され、server_port
-
アクセスhttp://gaia.cs.umass.edu/wireshark-labs/INTRO-wireshark-file1.html、HTMLがプロキシサーバを介してオブジェクトを要求された
プロキシサーバーがキャッシュされていないので、そのサーバーへのプロキシサーバーgaia.cs.umass.edu図中のオブジェクト要求しHost name: gaia.cs.umass.edu
、プロキシサーバーの存在とファイルオブジェクトフォルダ、次の結果を
-
プロキシサーバーを経由して、再度、リクエストオブジェクトをページをご覧ください
プロキシサーバは、オブジェクトのキャッシュを持っているので、オブジェクトは、このように、図では、ブラウザにキャッシュから直接取得しますRead 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>
直接ディスプレイ表示された場合、ファイルに加えて、応答メッセージエンティティ体、すなわちHTMLの外側部分、さらにステータス行、第一のライン部などを含む、ことを言及する価値があります
このプロセスは、オブジェクトとそののみ表示さ有益な情報を確実にするために、ブラウザに送信するか、ファイルオブジェクトを保存するときに処理されることを確信することができ
outputdata = f.readlines()
outputdata = outputdata[outputdata.index("<html>\n")-1:]
-
その他の考慮事項
プロキシサーバは、このマシンの時点で実行されているIPアドレスが設定されている場合、
127.0.0.1
それは正常に機能しない場合がありますソリューション:直接入力のヌル文字は、デフォルト値localhostを使用します
# tcpSerSock.bind(('127.0.0.1', server_port)) tcpSerSock.bind(('', server_port))