当前位置:网站首页>Python3 e-learning case 4: writing web proxy
Python3 e-learning case 4: writing web proxy
2020-11-06 01:17:00 【itread01】
Please go to Baidu Encyclopedia for the definition and function of proxy server ~
1. Web Proxy How to realize it
This is based on the previous article “ Write Web Server” It's written , The main logic is shown in the figure below :
What we're going to write is the middle Web Proxy part , When the client to Web Proxy Send a request for access to a web site (Request) When ,Web Proxy You will first check whether you have the requested file , If there is, go straight back to (Response), If not ,Web Proxy It's like Web Server( The server to access the web address ) Send a request to get the target file , And then to Client return .
2. Web Proxy Use
First , When we visit a website, in order to visit through proxy, we can't simply open the browser and enter the URL to visit ( That would be Client Direct to Web Server Transmit Reuest 了 ), Here you can download a file called Wget Tools for , This is for Web Proxy It's like jdk For Java The same thing ( Of course, there may be other tools that can access the proxy server first , There is no discussion about ), After downloading, you can unzip it and use it , It's like using jdk First, find the folder where the file is located in the command line window , If you do not want to input a list of directories to query each time, you can also add the path of this file to the environment variable ( How to configure self search ).
When Web Proxy and Wget When you're ready, you can start running :
First, execute Web Proxy The program , And then through Wget Request to use a proxy and deliver Request
(Wget command :wget xxx.xxx.xx -e use_proxy=on -e http_proxy=127.0.0.1:8000), among “xxx.xxx.xx” It's the URL you want to ask for
3. Web Proxy Source code
import socket
def handleReq(clientSocket):
# recv data
# find the fileName
# judge if the file named "fileName" if existed
# if not exists, send req to get it
recvData = clientSocket.recv(1024).decode()
fileName = recvData.split()[1].split("//")[1].replace('/', '')
print("fileName: " + fileName)
try:
file = open(fileName, 'rb')
print("File is found in proxy server.")
responseMsg = file.readlines()
for i in range(0, len(responseMsg)):
clientSocket.sendall(responseMsg[i])
print("Send, done.")
except Exception:
try:
print("File is not exist.\nSend request to server...")
proxyClientSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serverName = fileName.split(":")[0]
proxyClientSocket.connect((serverName, 80))
proxyClientSocket.sendall(recvData.encode())
responseMsg = proxyClientSocket.recv(4069)
print("File is found in server.")
proxyClientSocket.sendall(responseMsg)
print("Send, done.")
# cache
cache = open("./" + fileName, 'w')
cache.writelines(responseMsg.decode().replace('\r\n', '\n'))
cache.close()
print("Cache, done.")
except:
print("Connect timeout.")
def startProxy(port):
proxyServerSocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
proxyServerSocket.bind(("", port))
proxyServerSocket.listen(0)
while True:
try:
print("Proxy is waiting for connecting...")
clientSocket, addr = proxyServerSocket.accept()
print("Connect established")
handleReq(clientSocket)
clientSocket.close()
except Exception as e:
print("error: {0}".format(e))
break
proxyServerSocket.close()
if __name__ == '__main__':
while True:
try:
port = int(input("choose a port number over 1024:"))
except ValueError:
print("Please input an integer rather than {0}".format(type(port)))
continue
else:
if port <= 1024:
print("Please input an integer greater than 1024")
continue
else:
break
startProxy(port)
4. Wget tool kit
Link to :https://pan.baidu.com/s/1Ae2_Cq9SYbKnfhhyJ1VhpQ
Extraction code :awsl
&n
版权声明
本文为[itread01]所创,转载请带上原文链接,感谢
边栏推荐
- ipfs正舵者Filecoin落地正当时 FIL币价格破千来了
- 01 . Go语言的SSH远程终端及WebSocket
- Details of dapr implementing distributed stateful service
- Elasticsearch 第六篇:聚合統計查詢
- 100元扫货阿里云是怎样的体验?
- Filecoin主网上线以来Filecoin矿机扇区密封到底是什么意思
- Python自动化测试学习哪些知识?
- Skywalking series blog 1 - install stand-alone skywalking
- 向北京集结!OpenI/O 2020启智开发者大会进入倒计时
- 深度揭祕垃圾回收底層,這次讓你徹底弄懂她
猜你喜欢

熬夜总结了报表自动化、数据可视化和挖掘的要点,和你想的不一样

至联云分享:IPFS/Filecoin值不值得投资?

你的财务报告该换个高级的套路了——财务分析驾驶舱

中国提出的AI方法影响越来越大,天大等从大量文献中挖掘AI发展规律

Don't go! Here is a note: picture and text to explain AQS, let's have a look at the source code of AQS (long text)

合约交易系统开发|智能合约交易平台搭建

快快使用ModelArts,零基础小白也能玩转AI!

直播预告 | 微服务架构学习系列直播第三期

如何将数据变成资产?吸引数据科学家

Working principle of gradient descent algorithm in machine learning
随机推荐
熬夜总结了报表自动化、数据可视化和挖掘的要点,和你想的不一样
High availability cluster deployment of jumpserver: (6) deployment of SSH agent module Koko and implementation of system service management
简直骚操作,ThreadLocal还能当缓存用
Python自动化测试学习哪些知识?
直播预告 | 微服务架构学习系列直播第三期
如何在Windows Server 2012及更高版本中將域控制器降級
Leetcode's ransom letter
Filecoin主网上线以来Filecoin矿机扇区密封到底是什么意思
OPTIMIZER_ Trace details
选择站群服务器的有哪些标准呢?
人工智能学什么课程?它将替代人类工作?
How do the general bottom buried points do?
条码生成软件如何隐藏部分条码文字
CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
(2)ASP.NET Core3.1 Ocelot路由
至联云分享:IPFS/Filecoin值不值得投资?
Polkadot series (2) -- detailed explanation of mixed consensus
Microservices: how to solve the problem of link tracing
How to get started with new HTML5 (2)