To log on to Baidu, you must first capture the Baidu logon package. Because it is a webpage logon, httpwatch is the most convenient. The test account I use is itiandatest1 and the password is itianda. The packet capture result is as follows:
Copy codeThe Code is as follows: POST /? Log in 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
The logon package is captured, and the following code is written:
Copy codeThe Code is as follows: import socket
Import ssl
Sock = ssl. wrap_socket (socket. socket ())
Ssl is a module dedicated to https processing. We use the wrap_socket function of this module to generate an SSLSocket object.
Then establish the connection:Copy codeThe Code is as follows: sock. connect ('passport .baidu.com ', 443 ))
Note that https uses port 443 instead of port 80.
Send data later:
Copy codeThe Code is as follows: data = '''\
POST /? Log in 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 called after sendall.
The rest is no different from the common socket processing method:Copy codeThe Code is as follows: recv_data = sock. recv (8192)
Sock. close ()
Print recv_data
Because we only need cookie information, we can only receive a small amount of data.
If the logon succeeds, the server returns the set-cookie containing BDUSS:
Copy codeThe Code is as follows: HTTP/1.1 200 OK
Set-Cookie: BAIDUID = DB464E1EBA6571FB82D70460D6AAB666: FG = 1; max-age = 946080000; expires = Wed, 17:18:17 GMT; domain = .baidu.com; path =/; version = 1
P3P: CP = "oti dsp cor iva our ind com"
Date: Mon, 19 Dec 2011 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-aFpQQVFBQUFBJCQAAAAAAAAAAAouTSCLkioVaXRpYW5kYXRlc3QxAAAAAAAAAAAAAAAAAAAAAAAAAADgmoV5AAAAAOCahXkAAAAAuWZCAAAAAAAxMC42NS40NNlx707Zce9OWT; expires = Tue, 01 Jan 2030 00:00:00 GMT; path =/; domain = .baidu.com
Set-Cookie: PTOKEN = 16ba4a120f070f3cc759a817981c2516; expires = Tue, 01 Jan 2030 00:00:00 GMT; path =/; domain = passport.baidu.com; HttpOnly
Set-Cookie: token en = fda94395cd4ae4661cefd3a4017a8454; expires = Tue, 01 Jan 2030 00:00:00 GMT; path =/; domain = passport.baidu.com
Set-Cookie: USERID = 626167789a799e630e60fb27466fa80e; expires = Tue, 01 Jan 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. login successful.
This article is from: itianda's blog