帮助文档>代理IP>代码示例 > 隧道代理(动态请求)python语言代码示例

隧道代理(动态请求)python语言代码示例

发布时间:2023-03-27 21:39

普通模式

  1. import requests
  2. targetURL = "https://ip.cn/api/index?ip=&type=0" //要访问的目标地址
  3. proxyAddr = "请改成您的隧道地址" //如:tunnel3.qg.net:19263
  4. authKey = "请改成您的Key"
  5. password = "请改成您的AuthPwd"
  6. # 账密模式
  7. proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
  8. "user": authKey,
  9. "password": password,
  10. "server": proxyAddr,
  11. }
  12. proxies = {
  13. "http": proxyUrl,
  14. "https": proxyUrl,
  15. }
  16. for i in range(10):
  17. resp = requests.get(targetURL, proxies=proxies)
  18. print(resp.text)

打标记模式

  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 normal_tunnel():
  43. """
  44. 结果类似:
  45. request on normal mode
  46. session with headers: {'Proxy-Authorization': 'Basic xxxx'}
  47. request id: 1, code: 200, result: 140.250.149.229
  48. """
  49. reset_tunnel_proxy_headers()
  50. print("request on normal mode")
  51. resp = new_session().get(target_url, proxies=proxy)
  52. print("request id: 1, code: %s, result: %s" % (resp.status_code, resp.text))
  53. def mark_tunnel():
  54. """
  55. 结果类似:
  56. request with mark
  57. session with headers: {'Proxy-Authorization': 'Basic xxxx', 'Proxy-TunnelID': 'channel-1', 'Proxy-TTL': 10}
  58. request id: 1 , code: 200, result: 183.166.118.48
  59. request id: 2 , code: 200, result: 183.166.118.48
  60. request id: 3 , code: 200, result: 183.166.118.48
  61. request id: 4 , code: 200, result: 183.166.118.48
  62. request id: 5 , code: 200, result: 183.166.118.48
  63. request id: 6 , code: 200, result: 183.166.118.48
  64. request id: 7 , code: 200, result: 183.142.59.203
  65. request id: 8 , code: 200, result: 183.142.59.203
  66. request id: 9 , code: 200, result: 183.142.59.203
  67. request id: 10, code: 200, result: 123.54.235.89
  68. """
  69. reset_tunnel_proxy_headers()
  70. update_tunnel_proxy_headers("Proxy-TunnelID", "channel-1")
  71. update_tunnel_proxy_headers("Proxy-TTL", 10)
  72. se = new_session()
  73. print("request with mark")
  74. for i in range(1, 12):
  75. resp = se.get(target_url, proxies=proxy, headers={"Connection": "close"})
  76. print("request id: %-2s, code: %s, result: %s" % (i, resp.status_code, resp.text))
  77. time.sleep(1)
  78. if __name__ == "__main__":
  79. normal_tunnel()
  80. mark_tunnel()
本文导读

客户热线: