Python使用Socket(Https)Post登录百度的实现代码
程序员文章站
2022-04-20 18:21:14
登录百度,首先当然是先抓百度的登录包 ,由于是网页登录,最方便的自然是httpwatch了,我使用的测试账号是itiandatest1,密码是itianda,抓包结果: 复...
登录百度,首先当然是先抓百度的登录包 ,由于是网页登录,最方便的自然是httpwatch了,我使用的测试账号是itiandatest1,密码是itianda,抓包结果:
复制代码 代码如下:
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
登录包抓到了,下面开始写代码:
复制代码 代码如下:
import socket
import ssl
sock = ssl.wrap_socket(socket.socket())
ssl是专门用来处理https的模块,我们使用该模块的wrap_socket函数生成一个sslsocket对象。
然后建立连接:
复制代码 代码如下:
sock.connect(('passport.baidu.com', 443))
这里需要注意的是https使用443端口,不是80。
之后发送数据:
复制代码 代码如下:
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)
需要注意的是sendall之后不能调用shutdown方法。
其余部分就和普通的socket处理方式没什么差别了 :
复制代码 代码如下:
recv_data = sock.recv(8192)
sock.close()
print recv_data
由于我们只需要cookie信息,所以只接收少量数据就可以了。
登录成功的标志是服务器返回含有bduss的set-cookie:
复制代码 代码如下:
http/1.1 200 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, 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: stoken=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,登陆成功。
本文来自: itianda's blog