帮助文档>代理IP>代码示例 > 隧道代理(固定时长)python语言代码示例

隧道代理(固定时长)python语言代码示例

发布时间:2023-03-27 21:39
  1. import base64
  2. import time
  3. import requests
  4. from requests.adapters import HTTPAdapter
  5. auth_key = "请改成您的Key"
  6. password = "请改成您的AuthPwd"
  7. tunnel_server = "http://请改成您的隧道地址" # 如:tunnel3.qg.net:19263
  8. target_url = "https://ip.cn/api/index?ip=&type=0" # 要访问的目标地址
  9. proxy_headers = {}
  10. proxy = {
  11. "http": tunnel_server,
  12. "https": tunnel_server
  13. }
  14. def encode_authorization(key, passwd):
  15. # python 使用 bytes 类型进行 base64 编码
  16. basic_str = bytes("%s:%s" % (key, passwd), "ascii")
  17. # 得到的返回值也是 bytes 类型,所以需要再 decode 为字符串
  18. return "Basic %s" % base64.b64encode(basic_str).decode("utf-8")
  19. def reset_tunnel_proxy_headers():
  20. global proxy_headers
  21. proxy_headers = {
  22. tunnel_server: {
  23. "Proxy-Authorization": encode_authorization(auth_key, password)
  24. }
  25. }
  26. def update_tunnel_proxy_headers(key, val):
  27. global proxy_headers
  28. proxy_headers[tunnel_server][key] = val
  29. def new_session():
  30. adapter = TunnelProxyAdapter()
  31. se = requests.Session()
  32. se.mount('https://', adapter)
  33. se.mount('http://', adapter)
  34. return se
  35. class TunnelProxyAdapter(requests.adapters.HTTPAdapter):
  36. def proxy_headers(self, p):
  37. if p in proxy_headers:
  38. print("session with headers:", proxy_headers[p])
  39. return proxy_headers[p]
  40. else:
  41. return None
  42. def multi_channel_tunnel():
  43. """
  44. 结果类似:
  45. request on multi channel
  46. request id: 1 , channel id: channel-1, code: 200, result: 183.155.88.224
  47. request id: 2 , channel id: channel-2, code: 200, result: 125.112.38.153
  48. request id: 3 , channel id: channel-3, code: 200, result: 183.155.89.125
  49. request id: 4 , channel id: channel-4, code: 200, result: 49.71.121.169
  50. request id: 5 , channel id: channel-5, code: 200, result: 115.210.67.220
  51. request id: 6 , channel id: channel-6, code: 200, result: 36.25.41.178
  52. request id: 7 , channel id: channel-7, code: 200, result: 180.125.162.116
  53. request id: 8 , channel id: channel-8, code: 200, result: 140.250.150.158
  54. request id: 9 , channel id: channel-9, code: 200, result: 121.227.102.227
  55. request id: 10, channel id: channel-10, code: 200, result: 49.88.106.198
  56. request id: 1 , channel id: channel-1, code: 200, result: 183.155.88.224
  57. request id: 2 , channel id: channel-2, code: 200, result: 125.112.38.153
  58. request id: 3 , channel id: channel-3, code: 200, result: 183.155.89.125
  59. request id: 4 , channel id: channel-4, code: 200, result: 49.71.121.169
  60. request id: 5 , channel id: channel-5, code: 200, result: 115.210.67.220
  61. request id: 6 , channel id: channel-6, code: 200, result: 36.25.41.178
  62. request id: 7 , channel id: channel-7, code: 200, result: 180.125.162.116
  63. request id: 8 , channel id: channel-8, code: 200, result: 140.250.150.158
  64. request id: 9 , channel id: channel-9, code: 200, result: 121.227.102.227
  65. request id: 10, channel id: channel-10, code: 200, result: 49.88.106.198
  66. """
  67. print("request on multi channel")
  68. reset_tunnel_proxy_headers()
  69. for i in range(1, 11):
  70. se = new_session()
  71. chan_id = "channel-%s" % i
  72. update_tunnel_proxy_headers("Proxy-TunnelID", chan_id)
  73. resp = se.get(target_url, proxies=proxy, headers={"Connection": "close"})
  74. print("request id: %-2s, channel id: %s, code: %s, result: %s" % (i, chan_id, resp.status_code, resp.text))
  75. time.sleep(10)
  76. # 因为固定时长为1分钟,所以在1分钟内继续使用已有通道,仍是之前的IP
  77. for i in range(1, 11):
  78. se = new_session()
  79. chan_id = "channel-%s" % i
  80. update_tunnel_proxy_headers("Proxy-TunnelID", chan_id)
  81. resp = se.get(target_url, proxies=proxy, headers={"Connection": "close"})
  82. print("request id: %-2s, channel id: %s, code: %s, result: %s" % (i, chan_id, resp.status_code, resp.text))
  83. if __name__ == "__main__":
  84. multi_channel_tunnel()
本文导读

客户热线: