python模拟登录带cookie
2016-03-08 14:44
746 查看
#!/usr/bin/python
import sys, urllib2, getpass
import urllib
import urllib
import urllib2
import cookielib
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
class MyWeb():
def __init__(self):
self.header = {
'Host': 'music.163.com',
'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8",
'Referer': 'http://music.163.com/song?id=26599525',
"User-Agent": "Opera/8.0 (Macintosh; PPC Mac OS X; U; en)"
}
self.cookie = cookielib.LWPCookieJar()
self.cookie_support = urllib2.HTTPCookieProcessor(self.cookie)
self.opener = urllib2.build_opener(self.cookie_support,
urllib2.HTTPHandler)
urllib2.install_opener(self.opener)
def get(self, url):
req = urllib2.Request(url)
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)
if __name__ == "__main__":
web = MyWeb()
url = 'http://192.168.1.1'
res = web.get(url)
import sys, urllib2, getpass
import urllib
import urllib
import urllib2
import cookielib
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
class MyWeb():
def __init__(self):
self.header = {
'Host': 'music.163.com',
'Content-Type': "application/x-www-form-urlencoded; charset=UTF-8",
'Referer': 'http://music.163.com/song?id=26599525',
"User-Agent": "Opera/8.0 (Macintosh; PPC Mac OS X; U; en)"
}
self.cookie = cookielib.LWPCookieJar()
self.cookie_support = urllib2.HTTPCookieProcessor(self.cookie)
self.opener = urllib2.build_opener(self.cookie_support,
urllib2.HTTPHandler)
urllib2.install_opener(self.opener)
def get(self, url):
req = urllib2.Request(url)
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)
if __name__ == "__main__":
web = MyWeb()
url = 'http://192.168.1.1'
res = web.get(url)
相关文章推荐
- Python 资源
- 【python】编程语言入门经典100例--26
- Python——管理属性(2)
- python项目之 爬虫爬取煎蛋jandan的妹子图-上
- Python 知识结构图
- LeetCode----Lowest Common Ancestor of a Binary Search Tree
- Python爬取中文页面的时候出现的乱码问题
- 使用python的PIL库简单的处理图像
- 简体中文转繁体的python简单实现
- 常用Python第三方库 简介
- python 模拟web网页登录过程
- Python对字母字符(串)中大小写转换函数--upper() 和 lower()
- python 文件保存 出错
- 将爬取到的数据(用Python)写入PostgreSQL数据库
- Python——管理属性(1)
- python 系列之 - 多线程
- python学习日记-01
- python 类属性初始化
- python基础(正则表达式)
- python descriptor