验证Scrapy HTTP代理

时间:2011-11-20 17:01:12

标签: python django proxy scrapy http-authentication

我可以使用request.meta ['proxy']设置http代理,但如何验证代理呢?

这不能指定用户并传递:

request.meta['proxy'] = 'http://user:pass@123.456.2323:2222'

从四处看看,我可能不得不发送request.headers ['Proxy-Authorization'],但我发送的是什么格式?

1 个答案:

答案 0 :(得分:8)

用户名和密码是以“username:password”

的形式进行base64编码的
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