Python uses socket (HTTPS) post to login Baidu's implementation code _python

Source: Internet
Author: User

Login Baidu, the first of course is to catch Baidu's login package, because it is a Web login, the most convenient nature is httpwatch, I use the test account is itiandatest1, the password is Itianda, grab package results:

Copy Code code as follows:

POST/?login http/1.1
Accept:image/jpeg, Application/x-ms-application, Image/gif, Application/xaml+xml, Image/pjpeg, application/ X-MS-XBAP, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, */*
Referer:https://passport.baidu.com/?login&tpl=mn
Accept-language:zh-cn
user-agent:mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; trident/5.0; SLCC2. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0;. net4.0c;. net4.0e; Alexa Toolbar; BOIE9; ZHCN)
content-type:application/x-www-form-urlencoded
Accept-encoding:gzip, deflate
Host:passport.baidu.com
content-length:243
Connection:keep-alive
Cache-control:no-cache

Login pack caught, start writing code below:

Copy Code code as follows:

Import socket
Import SSL
Sock = Ssl.wrap_socket (Socket.socket ())

SSL is a module that is designed to handle HTTPS, and we use the Wrap_socket function of the module to generate a Sslsocket object.

Then establish the connection:
Copy Code code as follows:

Sock.connect ((' passport.baidu.com ', 443))

It should be noted that HTTPS uses 443 ports, not 80.

Send data after:

Copy Code code as follows:

data = ' "' \
POST/?login http/1.1
Accept:image/jpeg, Application/x-ms-application, Image/gif, Application/xaml+xml, Image/pjpeg, application/ X-MS-XBAP, Application/vnd.ms-excel, Application/vnd.ms-powerpoint, Application/msword, */*
Referer:https://passport.baidu.com/?login&tpl=mn
Accept-language:zh-cn
user-agent:mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.1; trident/5.0; SLCC2. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0;. net4.0c;. net4.0e; Alexa Toolbar; BOIE9; ZHCN)
content-type:application/x-www-form-urlencoded
Host:passport.baidu.com
content-length:243
Connection:keep-alive
Cache-control:no-cache
tpl_ok=&next_target=&tpl=mn&skip_ok=&aid=&need_pay=&need_coin=&pay_method=&u= Http%3a%2f%2fwww.baidu.com%2f&return_method=get&more_param=&return_type=&psp_tt=0&password =itianda&safeflg=0&isphone=tpl&username=itiandatest1&verifycode=&mem_pass=on\
'''
Sock.sendall (data)

Note that the shutdown method cannot be invoked after Sendall.

The rest is no different from normal socket processing:
Copy Code code as follows:

Recv_data = Sock.recv (8192)
Sock.close ()
Print Recv_data

Since we only need cookie information, we can only receive a small amount of data.

The sign of successful login is that the server returns Set-cookie containing Bduss:

Copy Code code as follows:

http/1.1 OK
set-cookie:baiduid=db464e1eba6571fb82d70460d6aab666:fg=1; max-age=946080000; expires=wed, 11-dec-41 17:18:17 GMT; domain=.baidu.com; path=/; Version=1
p3p:cp= "OTI DSP COR IVA our IND COM"
Date:mon, Dec 17:18:17 GMT
Server:apache
p3p:cp= "OTI DSP COR IVA our IND COM"
p3p:cp= "OTI DSP COR IVA our IND COM"
p3p:cp= "OTI DSP COR IVA our IND COM"
set-cookie:baiduid=26fd0cb5389bf4699c447982d8080239:fg=1; expires=wed, 11-dec-41 17:18:17 GMT; max-age=946080000; path=/; domain=.baidu.com; Version=1
set-cookie:baiduid=26fd0cb5389bf4698191e4134cacea29:fg=1; expires=wed, 11-dec-41 17:18:17 GMT; max-age=946080000; path=/; domain=.baidu.com; Version=1
set-cookie:bduss= Dtajkzwtfwr3hxt3jsc09ldknsz011ylzka340vwtqnkzzbw0tutdoufp-afpqqvfbqufbjcqaaaaaaaaaaaoutsclkiovaxrpyw5kyxrlc3qxaaaaaaaaaaa Aaaaaaaaaaaaaaadgmov5aaaaaocahxkaaaaauwzcaaaaaaaxmc42ns40nnlx707zce9owt; Expires=tue, 2030 00:00:00 GMT; path=/; Domain=.baidu.com
set-cookie:ptoken=16ba4a120f070f3cc759a817981c2516; Expires=tue, 2030 00:00:00 GMT; path=/; domain=passport.baidu.com; HttpOnly
set-cookie:stoken=fda94395cd4ae4661cefd3a4017a8454; Expires=tue, 2030 00:00:00 GMT; path=/; Domain=passport.baidu.com
set-cookie:userid=626167789a799e630e60fb27466fa80e; Expires=tue, 2030 00:00:00 GMT; path=/; Domain=.baidu.com
Content-type:text/html;charset=gbk
Cache-control:no-cache
Pragma:no-cache
Content-encoding:none
content-length:850
Connection:close

OK, landing successful.
This article from: Itianda ' s blog

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.