python - 验证 Scrapy HTTP 代理

标签 python django proxy scrapy http-authentication

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

这对指定用户和密码不起作用:

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

环顾四周,我可能必须发送 request.headers['Proxy-Authorization'],但我应该以什么格式发送它?

最佳答案

用户名和密码以“用户名:密码”的形式进行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

关于python - 验证 Scrapy HTTP 代理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8203093/

相关文章:

python - 在 pandas df 中创建新列并有条件地分配字符串值

python - Pyinstaller 和 Django Rest

Android : Intercept network calls (HTTP, HTTPS 等..)源 self 的应用程序内的第 3 方库

python - 在恒定时间内检索堆栈中的最小元素

python - 分配规则

python - 使用 django 的多个应用程序

python - 外键属性上的 Django Admin 过滤器

https - 当我使用 reqwest 的客户端生成器时,有什么方法可以向代理添加基本身份验证?

http - 故障排除 "Request Entity Too Large"(HTTP 413) 错误消息返回到浏览器

Python正则表达式与字符串匹配的问题