python 模拟web网页登录过程
2016-03-08 13:25
696 查看
#!/usr/bin/python
import sys, urllib2, getpass
class TerminalPassword(urllib2.HTTPPasswordMgr):
def find_user_password(self, realm, authurl):
retval = urllib2.HTTPPasswordMgr.find_user_password(self, realm, authurl)
if retval[0] == None and retval[1] == None:
sys.stdout.write("Login required for %s at %s\n" % (realm, authurl))
sys.stdout.write("Username: ")
username = sys.stdin.readline().rstrip()
password = getpass.getpass().rstrip()
return (username, password)
else:
return retval
req = urllib2.Request(sys.argv[1])
opener = urllib2.build_opener(urllib2.HTTPBasicAuthHandler(TerminalPassword()))
fd = opener.open(req)
print "Retrieved", fd.geturl()
info = fd.info()
for key, value in info.items():
print "%s = %s" % (key, value)
相关文章推荐
- Python对字母字符(串)中大小写转换函数--upper() 和 lower()
- python 文件保存 出错
- 将爬取到的数据(用Python)写入PostgreSQL数据库
- Python——管理属性(1)
- python 系列之 - 多线程
- python学习日记-01
- python 类属性初始化
- python基础(正则表达式)
- python descriptor
- Python Selenium环境配置
- python 切片
- Python hashmap
- Python Singleton
- Python实现单例类
- python3 查找文章中的高频词汇
- python中.py和.pyw文件的区别
- Numpy使用2
- Python自动化开发基础 函数-模块-正则-迭代器 笔记
- 使用monkeyrunner实现手机自动转账功能
- Python 点滴 I