隧道代理(动态请求)python语言代码示例
发布时间:2023-03-27 21:39
普通模式
import requests
targetURL = "https://ip.cn/api/index?ip=&type=0" //要访问的目标地址
proxyAddr = "请改成您的隧道地址" //如:tunnel3.qg.net:19263
authKey = "请改成您的Key"
password = "请改成您的AuthPwd"
# 账密模式
proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
"user": authKey,
"password": password,
"server": proxyAddr,
}
proxies = {
"http": proxyUrl,
"https": proxyUrl,
}
for i in range(10):
resp = requests.get(targetURL, proxies=proxies)
print(resp.text)
打标记模式
import base64
import time
import requests
from requests.adapters import HTTPAdapter
auth_key = "请改成您的Key"
password = "请改成您的AuthPwd"
tunnel_server = "http://请改成您的隧道地址" //如:tunnel3.qg.net:19263
target_url = "https://ip.cn/api/index?ip=&type=0" // 要访问的目标地址
proxy_headers = {}
proxy = {
"http": tunnel_server,
"https": tunnel_server
}
def encode_authorization(key, passwd):
# python 使用 bytes 类型进行 base64 编码
basic_str = bytes("%s:%s" % (key, passwd), "ascii")
# 得到的返回值也是 bytes 类型,所以需要再 decode 为字符串
return "Basic %s" % base64.b64encode(basic_str).decode("utf-8")
def reset_tunnel_proxy_headers():
global proxy_headers
proxy_headers = {
tunnel_server: {
"Proxy-Authorization": encode_authorization(auth_key, password)
}
}
def update_tunnel_proxy_headers(key, val):
global proxy_headers
proxy_headers[tunnel_server][key] = val
def new_session():
adapter = TunnelProxyAdapter()
se = requests.Session()
se.mount('https://', adapter)
se.mount('http://', adapter)
return se
class TunnelProxyAdapter(requests.adapters.HTTPAdapter):
def proxy_headers(self, p):
if p in proxy_headers:
print("session with headers:", proxy_headers[p])
return proxy_headers[p]
else:
return None
def normal_tunnel():
"""
结果类似:
request on normal mode
session with headers: {'Proxy-Authorization': 'Basic xxxx'}
request id: 1, code: 200, result: 140.250.149.229
"""
reset_tunnel_proxy_headers()
print("request on normal mode")
resp = new_session().get(target_url, proxies=proxy)
print("request id: 1, code: %s, result: %s" % (resp.status_code, resp.text))
def mark_tunnel():
"""
结果类似:
request with mark
session with headers: {'Proxy-Authorization': 'Basic xxxx', 'Proxy-TunnelID': 'channel-1', 'Proxy-TTL': 10}
request id: 1 , code: 200, result: 183.166.118.48
request id: 2 , code: 200, result: 183.166.118.48
request id: 3 , code: 200, result: 183.166.118.48
request id: 4 , code: 200, result: 183.166.118.48
request id: 5 , code: 200, result: 183.166.118.48
request id: 6 , code: 200, result: 183.166.118.48
request id: 7 , code: 200, result: 183.142.59.203
request id: 8 , code: 200, result: 183.142.59.203
request id: 9 , code: 200, result: 183.142.59.203
request id: 10, code: 200, result: 123.54.235.89
"""
reset_tunnel_proxy_headers()
update_tunnel_proxy_headers("Proxy-TunnelID", "channel-1")
update_tunnel_proxy_headers("Proxy-TTL", 10)
se = new_session()
print("request with mark")
for i in range(1, 12):
resp = se.get(target_url, proxies=proxy, headers={"Connection": "close"})
print("request id: %-2s, code: %s, result: %s" % (i, resp.status_code, resp.text))
time.sleep(1)
if __name__ == "__main__":
normal_tunnel()
mark_tunnel()
相关文章