Python 设置 IP 代理 访问网页 ( 用户名密码验证代理 )
2016-08-05 15:17
801 查看
1. Python 的 urllib2 设置带有密码验证的代理服务器,简洁写法如下:
proxy_handler = urllib2.ProxyHandler({'http': 'username:psw@IP:port'}) opener = urllib2.build_opener(proxy_handler) urllib2.install_opener(opener) response = urllib2.urlopen('http://google.com') html = response.read()
此种写法中,代理部分urllib2.ProxyHandler({'http': 'username:psw@IP:port'})的
'username:psw@IP:port' [code]没有添加“http://”, 但是效果相同!
2. urllib2的opener中的 handler 默认值当中,已经有HTTPHandler 所以 如下的写法中[/code]
import urllib2proxy_support = urllib2.ProxyHandler({'http':'http://usename:psw@IP:port'})auth = urllib2.HTTPBasicAuthHandler()opener = urllib2.build_opener(proxy_support, auth, urllib2.HTTPHandler)urllib2.install_opener(opener)response = urllib2.urlopen('http://google.com')html = response.read()后面添加的 HTTPBasicAuthHandler, urllib2.HTTPHandler 实际上并没有额外的作用! 第一种的简洁写法已经可以使用!
相关文章推荐
- Python算法之---冒泡,选择,插入排序算法
- getattr(),setattr(),delattr(),hasattr()
- dict 字典的相关操作
- @classmethod
- 'ascii' codec can't decode byte 0xe9 in position 2 ordinal not in range
- Python 正则表达式(2)作业
- python学习初体验——如何编写和运行程序
- Python 正则表达式(2)
- Python的time模块datetime模块
- 用Python在训练好的log文件中提取出数据并画图
- 利用Python实现命令行版的火车票查看器
- python分布式进程传送脚本文件并执行
- Python-数据库操作-pymysql
- [LeetCode]--143. Reorder List(Python + Java)
- Python复习 基础知识
- python读写sqlite3数据库并将统计数据写入excel
- 认识Python
- 认识Python
- python datetime
- [Python]Compare __repr__ & __str__