proxytools

- collection of scripts for harvesting & testing proxies
git clone git://git.acid.vegas/proxytools.git
Log | Files | Refs | Archive | README | LICENSE

pythonproxy.md (4281B)

      1 # Proxy usage with Python
      2 
      3 ## [aiosocks](https://pypi.org/project/aiosocks/)
      4 
      5 ```python
      6 import asyncio
      7 import aiosocks
      8 
      9 async def proxy_example(proxy: str, use_ssl:  bool = False):
     10     '''Proxy can be in IP:PORT format or USER:PASS@IP:PORT format'''
     11 
     12     auth = proxy.split('@')[0].split(':') if '@' in proxy else None
     13     proxy_ip, proxy_port = proxy.split('@')[1].split(':') if '@' in proxy else proxy.split(':')
     14 
     15     options = {
     16         'proxy'      : aiosocks.Socks5Addr(proxy_ip, proxy_port),
     17         'proxy_auth' : aiosocks.Socks5Auth(*auth) if auth else None,
     18         'dst'        : (host, port),
     19         'limit'      : 1024,
     20         'ssl'        : ssl._create_unverified_context() if use_ssl else None,
     21         'family'     : 2 # 2 = IPv4 | 10 = IPv6
     22     }
     23 
     24     reader, writer = await asyncio.wait_for(aiosocks.open_connection(**options), 15) # 15 second timeout
     25 
     26     while True:
     27         data  = await asyncio.wait_for(reader.readuntil(b'\r\n'), 300) # 5 minute timeout on no data received
     28         print(data.decode().strip()) # Print the response from the server
     29 ```
     30 
     31 ## [aiohttp](https://pypi.org/project/aiohttp)
     32 ```python
     33 import asyncio
     34 import aiohttp
     35 
     36 async def proxy_example(proxy: str, url: str):
     37     '''Proxy can be in IP:PORT format or USER:PASS@IP:PORT format'''
     38 
     39     async with aiohttp.ClientSession() as session:
     40         async with session.get('https://google.com', proxy=f'http://{proxy}', timeout=15) as response:
     41             if response.status == 200: # 200 = success
     42                 print(response.text()) # Print the response from the server
     43 ```
     44 
     45 ## [http.client](https://docs.python.org/3/library/http.client.html)
     46 I really don't use this library much at all, so this is some LM generated function...
     47 
     48 ```python
     49 import base64
     50 import http.client
     51 
     52 def proxy_example(proxy: str, url):
     53     '''Proxy can be in IP:PORT format or USER:PASS@IP:PORT format'''
     54 
     55     auth = proxy.split('@')[0].split(':') if '@' in proxy else None
     56     proxy_host, proxy_port = proxy.split('@')[1].split(':') if '@' in proxy else proxy.split(':')
     57 
     58     scheme, rest = url.split('://', 1)
     59     host, path = rest.split('/', 1)
     60     path = '/' + path
     61     
     62     if scheme == 'https':
     63         conn = http.client.HTTPConnection(proxy_host, proxy_port)
     64         conn.request('CONNECT', host)
     65         response = conn.getresponse()
     66         if response.status != 200:
     67             print("Failed to establish a tunnel via proxy.")
     68             print(response.status, response.reason)
     69             return
     70                 conn = http.client.HTTPSConnection(proxy_host, proxy_port, context=None)
     71         conn.set_tunnel(host)
     72     else:
     73         conn = http.client.HTTPConnection(proxy_host, proxy_port)
     74         path = url
     75 
     76     headers = {}
     77     if auth:
     78         auth = base64.b64encode(f'{auth[0]}:{auth[1]}'.encode()).decode()
     79         headers['Proxy-Authorization'] = f'Basic {auth}'
     80 
     81 
     82     conn.request('GET', path, headers=headers)
     83     response = conn.getresponse()
     84     print(response.status, response.reason)
     85     if response.status == 200:
     86         data = response.read()
     87         print(data.decode())
     88 
     89     conn.close()
     90 ```
     91 
     92 ## [requests](https://pypi.org/project/requests/)
     93 ```python
     94 import requests
     95 
     96 def proxy_example(proxy: str, url: str):
     97     '''Proxy can be in IP:PORT format or USER:PASS@IP:PORT format'''
     98 
     99     proxy_handler = {'http': 'http://'+proxy, 'https': 'https://'+proxy}
    100     response = requests.get(url, proxies=proxies)
    101     print(response.text)
    102 ```
    103 
    104 ## [urllib.request](https://docs.python.org/3/library/urllib.html)
    105 ```python
    106 import urllib.request
    107 
    108 def proxy_example(proxy: str, url: str):
    109     '''Proxy can be in IP:PORT format or USER:PASS@IP:PORT format'''
    110 
    111     proxy_handler = urllib.request.ProxyHandler({'http': proxy, 'https': proxy})
    112     opener = urllib.request.build_opener(proxy_handler)
    113 
    114     if '@' in proxy: # Handle authentication
    115         creds, address = proxy.split('@')
    116         username, password = creds.split(':')
    117         auth_header = urllib.request.HTTPBasicAuthHandler()
    118         auth_header.add_password(realm=None, uri=proxy, user=username, passwd=password)
    119         opener.add_handler(auth_header)
    120 
    121     urllib.request.install_opener(opener)
    122 
    123     response = urllib.request.urlopen(url, timeout=15)
    124     if response.code == 200:
    125         print(response.read().decode())
    126 ```