Skip to content

Instantly share code, notes, and snippets.

@liquidgenius
Last active October 3, 2020 15:58
Show Gist options
  • Save liquidgenius/84b60fd5c3817ce213d2c33e5b45ce4c to your computer and use it in GitHub Desktop.
Save liquidgenius/84b60fd5c3817ce213d2c33e5b45ce4c to your computer and use it in GitHub Desktop.
Proxymesh is a Python function that proxies requests through Proxymesh's services. Just pass a qualified url and receive a Request's Response-like object in response. No error capturing is included.
#!/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 proxy
# print(proxy('https://proxymesh.com').text)
url = 'https://proxymesh.com'
response = Proxymesh(url)
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment