Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authenticate Scrapy HTTP Proxy

I can set an http proxy using request.meta['proxy'], but how do I authenticate the proxy?

This does not work to specify user and pass:

request.meta['proxy'] = 'http://user:[email protected]:2222'

From looking around, I may have to send request.headers['Proxy-Authorization'], but what format do I send it in?

like image 993
Lionel Avatar asked Nov 20 '11 17:11

Lionel


1 Answers

username and password are base64 encoded in the form "username:password"

import base64

# Set the location of the proxy
proxy_string = choice(self._get_proxies_from_file('proxies.txt')) # user:pass@ip:port
proxy_items = proxy_string.split('@')
request.meta['proxy'] = "http://%s" % proxy_items[1]

# setup basic authentication for the proxy
user_pass=base64.encodestring(proxy_items[0])
request.headers['Proxy-Authorization'] = 'Basic ' + user_pass
like image 77
Lionel Avatar answered Sep 28 '22 04:09

Lionel