帮助文档>代码示例 > Python语言代码示例

Python语言代码示例

发布时间:2022-07-03 00:46

Python requests

  1. import requests
  2.  
  3. targetURL = "http://myip.ipip.net"
  4. proxyAddr = "219.151.125.106:31615"
  5. authKey = "895314XY"
  6. password = "24D6YB309ZCB"
  7.  
  8. # 账密模式
  9. proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
  10. "user": authKey,
  11. "password": password,
  12. "server": proxyAddr,
  13. }
  14.  
  15. proxies = {
  16. "http": proxyUrl,
  17. "https": proxyUrl,
  18. }
  19.  
  20. resp = requests.get(targetURL, proxies=proxies)
  21. print(resp.text)

Python aiohttp

  1. import aiohttp,asyncio
  2.  
  3. targetURL = "http://myip.ipip.net"
  4. proxyAddr = "219.151.125.106:31615"
  5. authKey = "895314XY"
  6. password = "24D6YB309ZCB"
  7.  
  8. # 账密模式
  9. proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
  10. "user": authKey,
  11. "password": password,
  12. "server": proxyAddr,
  13. }
  14.  
  15. async def entry():
  16. conn = aiohttp.TCPConnector(ssl=False)
  17.  
  18. async with aiohttp.ClientSession(connector=conn) as session:
  19. async with session.get(targetURL, proxy=proxyUrl) as resp:
  20. body = await resp.read()
  21. print(resp.status)
  22. print(body)
  23.  
  24. loop = asyncio.get_event_loop()
  25. loop.run_until_complete(entry())
  26. loop.run_forever()

Python urllib2、urllib

  1. import urllib2
  2.  
  3. targetURL = "http://myip.ipip.net"
  4. proxyAddr = "219.151.125.106:31615"
  5. authKey = "895314XY"
  6. password = "24D6YB309ZCB"
  7.  
  8. proxyUrl = "http://%(user)s:%(password)s@%(server)s" % {
  9. "user": authKey,
  10. "password": password,
  11. "server": proxyAddr,
  12. }
  13.  
  14. proxies = urllib2.ProxyHandler({
  15. "http": proxyUrl,
  16. "https": proxyUrl,
  17. })
  18.  
  19. opener = urllib2.build_opener(proxies)
  20. urllib2.install_opener(opener)
  21. resp = urllib2.urlopen(targetURL).read()
  22.  
  23. print(resp)
本文导读