Skip to content

Instantly share code, notes, and snippets.

@wtrevino
Last active January 31, 2016 18:19
Show Gist options
  • Save wtrevino/c4ff3918914e4a80b793 to your computer and use it in GitHub Desktop.
Save wtrevino/c4ff3918914e4a80b793 to your computer and use it in GitHub Desktop.
How to set up Scrapy middleware with Proxymesh (or any other proxy service really...)
class ProxyMeshMiddleware(object):
def process_request(self, request, spider):
endpoint = os.environ.get('PROXY_ENDPOINT')
user = os.environ.get('PROXY_USER')
pw = os.environ.get('PROXY_PW')
if 'proxy' in request.meta:
return
request.meta['proxy'] = endpoint
proxy_user_pass = '{}:{}'.format(user, pw)
encoded_user_pass = base64.encodestring(proxy_user_pass)
request.headers['Proxy-Authorization'] = 'Basic ' + encoded_user_pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment