Requests Requests login and login using the login Cookies under Python3.6 under Python3.6 Cookies and use login

Requests login and use Cookies to log in Python3.6

 

The use of Python modules can be achieved Requests Post, Get functions such as send, I log on to a Web site, for example, record sends the user name, password and a verification code using graphical Post, as well as the content of Cookies by direct login.

1. Using POST sends the username, password and verification code. Here no identification code verification, validation test image had to be read, a manual input.

Copy the code
 1 def LoginByPost():
 2     imgUrl='http://***/authcode.php'
 3     s=requests.session()
 4     res=s.get(imgUrl,stream=True)
 5     im=Image.open(BytesIO(res.content))
 6     im.show()
 7     code=input()
 8     loginUrl='http://***/admin_loginCheck.php'
 9     postData={'pname':'admin','password':'***','validateCode':code}
10     rs=s.post(loginUrl,postData)
11 url='http://***/***/admin_honor.php' 12 res=s.get(url)
13 res.encoding='utf-8'
14 print(res.text)

Copy the code

At this point, we can see that we have successfully logged in, and output the contents of the specified page.

2. Use Cookies log in directly. No user name, password and a verification code. At this point, you need to get the site after logging Cookies, Cookies One method is to look through the browser, another method is to use the above requests.session get Cookies after login. Our second approach.

(1) obtain Cookies by requests.session.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def  GetCookie():
     imgUrl = 'http://***/authcode.php'
     s = requests.session()
     print (s.cookies.get_dict()) #先打印一下,此时一般应该是空的。
     res = s.get(imgUrl,stream = True )
     im = Image. open (BytesIO(res.content))
     im.show()
     code = input ()
     loginUrl = 'http://***/admin_loginCheck.php'
     postData = { 'pname' : 'admin' , 'password' : '***' , 'validateCode' :code}
     rs = s.post(loginUrl,postData)
     c = requests.cookies.RequestsCookieJar() #利用RequestsCookieJar获取
     c. set ( 'cookie-name' , 'cookie-value' )
     s.cookies.update(c)
     print (s.cookies.get_dict())

 (2) using the acquired log in directly above Cookies

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def  DirLogin():
     s = requests.session()
     url = 'http://***/***/admin_honor.php'
     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,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2' ,
     'Cache-Control' 'max-age=0' ,
     'Connection' 'keep-alive' ,
     'Host' '***' ,
     'Referer' 'http://***/***/admin_index.php'
     }
     cookies = { 'PHPSESSID' 'cnguud4r1hmn3passs906odp21' } #这里就是利用上面的函数获得的Cookies
     rs = s.get(url,headers = headers,cookies = cookies,verify = False )
     rs.encoding = 'utf-8'
     print (rs.text)

 At this point, you can directly view the contents of the page required.

Description: Ran and eggs, you will find that log in directly through the Cookies, sometimes useful, sometimes useless. The reason in this site is carried out by the server to the client Session judgment, while Session on the server side tend to set the session time limit, if it is time, the server will delete this Session, then, you have to use the first function again Cookie acquisition.

The use of Python modules can be achieved Requests Post, Get functions such as send, I log on to a Web site, for example, record sends the user name, password and a verification code using graphical Post, as well as the content of Cookies by direct login.

1. Using POST sends the username, password and verification code. Here no identification code verification, validation test image had to be read, a manual input.

Copy the code
 1 def LoginByPost():
 2     imgUrl='http://***/authcode.php'
 3     s=requests.session()
 4     res=s.get(imgUrl,stream=True)
 5     im=Image.open(BytesIO(res.content))
 6     im.show()
 7     code=input()
 8     loginUrl='http://***/admin_loginCheck.php'
 9     postData={'pname':'admin','password':'***','validateCode':code}
10     rs=s.post(loginUrl,postData)
11 url='http://***/***/admin_honor.php' 12 res=s.get(url)
13 res.encoding='utf-8'
14 print(res.text)

Copy the code

At this point, we can see that we have successfully logged in, and output the contents of the specified page.

2. Use Cookies log in directly. No user name, password and a verification code. At this point, you need to get the site after logging Cookies, Cookies One method is to look through the browser, another method is to use the above requests.session get Cookies after login. Our second approach.

(1) obtain Cookies by requests.session.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def  GetCookie():
     imgUrl = 'http://***/authcode.php'
     s = requests.session()
     print (s.cookies.get_dict()) #先打印一下,此时一般应该是空的。
     res = s.get(imgUrl,stream = True )
     im = Image. open (BytesIO(res.content))
     im.show()
     code = input ()
     loginUrl = 'http://***/admin_loginCheck.php'
     postData = { 'pname' : 'admin' , 'password' : '***' , 'validateCode' :code}
     rs = s.post(loginUrl,postData)
     c = requests.cookies.RequestsCookieJar() #利用RequestsCookieJar获取
     c. set ( 'cookie-name' , 'cookie-value' )
     s.cookies.update(c)
     print (s.cookies.get_dict())

 (2) using the acquired log in directly above Cookies

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
def  DirLogin():
     s = requests.session()
     url = 'http://***/***/admin_honor.php'
     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,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2' ,
     'Cache-Control' 'max-age=0' ,
     'Connection' 'keep-alive' ,
     'Host' '***' ,
     'Referer' 'http://***/***/admin_index.php'
     }
     cookies = { 'PHPSESSID' 'cnguud4r1hmn3passs906odp21' } #这里就是利用上面的函数获得的Cookies
     rs = s.get(url,headers = headers,cookies = cookies,verify = False )
     rs.encoding = 'utf-8'
     print (rs.text)

 At this point, you can directly view the contents of the page required.

Description: Ran and eggs, you will find that log in directly through the Cookies, sometimes useful, sometimes useless. The reason in this site is carried out by the server to the client Session judgment, while Session on the server side tend to set the session time limit, if it is time, the server will delete this Session, then, you have to use the first function again Cookie acquisition.

Guess you like

Origin www.cnblogs.com/durcaidurcai/p/11446510.html