Skip to content

Instantly share code, notes, and snippets.

@liquidgenius
Last active October 3, 2020 15:58
Show Gist options
  • Select an option

  • Save liquidgenius/84b60fd5c3817ce213d2c33e5b45ce4c to your computer and use it in GitHub Desktop.

Select an option

Save liquidgenius/84b60fd5c3817ce213d2c33e5b45ce4c to your computer and use it in GitHub Desktop.

Revisions

  1. liquidgenius revised this gist Oct 3, 2020. 1 changed file with 2 additions and 1 deletion.
    3 changes: 2 additions & 1 deletion proxy.py
    Original file line number Diff line number Diff line change
    @@ -34,7 +34,8 @@ def Proxymesh(url, proxy_choices=None, headers=None, country='US'):

    """
    # EXAMPLE USAGE
    # from module.util.proxy import Proxymesh as proxify
    # from module.util.proxy import Proxymesh as proxy
    # print(proxy('https://proxymesh.com').text)
    url = 'https://proxymesh.com'
    response = Proxymesh(url)
  2. liquidgenius created this gist Oct 3, 2020.
    41 changes: 41 additions & 0 deletions proxy.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,41 @@
    #!/usr/bin/env python
    # -*- coding: utf-8 -*-

    from random import choice

    import httpx
    import fake_useragent


    def Proxymesh(url, proxy_choices=None, headers=None, country='US'):
    """ Proxies requests through random servers. Pass a qualified url, it will be proxied through Proxymesh's
    service. If you provide a header dictionary, it will be merged with the Proxymesh required headers. Footprint is
    defaulted to USA. Change the proxy_choices and country parameter for other options. Your requesting IP address must
    first be authorized at Proxymesh: https://proxymesh.com/account/edit_ips/ """

    fallback = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.121 Safari/537.36'
    ua = fake_useragent.UserAgent(fallback=fallback)

    if proxy_choices is None:
    proxy_choices = ['us-il.proxymesh.com:31280', 'open.proxymesh.com:31280']

    proxy = choice(proxy_choices)
    proxies = {'http': f'http://{proxy}', 'https': f'http://{proxy}'}
    default_headers = {'user-agent': ua.random, 'X-ProxyMesh-Country': country}

    if headers is None:
    headers = default_headers
    else:
    headers = {**headers, **default_headers}

    with httpx.Client(headers=headers, proxies=proxies) as client:
    result = client.get(url)
    return result

    """
    # EXAMPLE USAGE
    # from module.util.proxy import Proxymesh as proxify
    url = 'https://proxymesh.com'
    response = Proxymesh(url)
    """