python 使用链式代理

一般情况下加代理只加一个,让真正访问目标网站的IP是代理的IP,

代理链的用法可以使用第一个代理跳出墙,第一个代理连接第二个代理,绕过复杂的网络环境。

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


import socket
import pyChainedProxy as socks #import pyChainedProxy
import requests
# Enable debugging
def DEBUG(msg):
print (msg)

socks.DEBUG = DEBUG


print("Check IP w/o proxyfying: ",requests.get('http://ip-api.com/json').content)

# Configure a default chain
chain = [
'socks5://localhost:7890/',
'http://testmidu-zone-adam:testmidu@43.153.102.235:6004'

]
socks.setdefaultproxy()

for hop in chain:
socks.adddefaultproxy(*socks.parseproxy(hop))

# Configure alternate routes (No proxy for localhost)
socks.setproxy('localhost', socks.PROXY_TYPE_NONE)
socks.setproxy('127.0.0.1', socks.PROXY_TYPE_NONE)


# Monkey Patching whole socket class (everything will be proxified)
rawsocket = socket.socket
socket.socket = socks.socksocket
# Everything will be proxied!

print("Check IP After proxyfying: ",requests.get('http://ip-api.com/json').content)


python 使用链式代理
https://kingjem.github.io/2024/11/14/python 链式代理使用/
作者
Ruhai
发布于
2024年11月14日
许可协议