Last active
August 29, 2015 14:06
-
-
Save cocoajin/c21972d8f91e88427d39 to your computer and use it in GitHub Desktop.
urllib2 http post 请求,cookie 管理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
''' | |
Created on 2014年9月17日 | |
@author: cocoajin | |
test | |
''' | |
import urllib,urllib2,cookielib | |
host = 'https://console.oray.com/passport/login' | |
values = { | |
'account':'uname', | |
'password':'pwd', | |
"url":'https://console.oray.com', | |
} | |
headers = { | |
'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', | |
'Accept-Encoding':'gzip, deflate', | |
'Accept-Language':'zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3', | |
'Connection':'keep-alive', | |
'Host':"console.oray.com", | |
'Referer':"https://console.oray.com/passport/login?url=https%3A%2F%2Fconsole.oray.com", | |
'User-Agent':"Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0", | |
'Content-Type':'application/x-www-form-urlencoded', | |
} | |
parms = urllib.urlencode(values) | |
#https post ,no cookie handle | |
#f=urllib2.urlopen(host, parms) | |
#print f.read() | |
#https post ,no cookie handle | |
#req=urllib2.Request(host,parms) | |
#req.add_header('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0') | |
#res=urllib2.urlopen(req) | |
#print res.read() | |
#https post,with cookie handle | |
cj = cookielib.CookieJar() | |
opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj)) | |
opener.addheaders=[('User-Agent','Mozilla/5.0 (Macintosh; Intel Mac OS X 10.9; rv:32.0) Gecko/20100101 Firefox/32.0')] | |
re=opener.open(host, parms) | |
print re.read() | |
#https post,widh cookie handle | |
#request = urllib2.Request(host, parms, headers) | |
#cookies = cookielib.CookieJar() | |
#cookie_handler= urllib2.HTTPCookieProcessor(cookies) | |
#redirect_handler= urllib2.HTTPRedirectHandler() | |
#opener = urllib2.build_opener(redirect_handler,cookie_handler) | |
#response = opener.open(request) | |
#print response.read() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment