Python3 using cookies to impersonate a login

Source: Internet
Author: User
Tags sublime text

Python3 using cookies to impersonate a login

using cookies To simulate a login step:

1. in the browser input http://demo.bxcker.com , enter a user name and password to log in.

2. Login to the success point of the "Customer management" module.

3. Enter the customer management module to display the customer list.

4. Capture the customer list by grabbing the package tool and get the Cookie information after login.

GET http://demo.bxcker.com/customer/index.shtml http/1.1

Host:demo.bxcker.com

Connection:keep-alive

Pragma:no-cache

Cache-control:no-cache

Accept: */*

X-requested-with:xmlhttprequest

user-agent:mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/66.0.3359.139 safari/537.36

referer:http://demo.bxcker.com/

Accept-encoding:gzip, deflate

accept-language:zh-cn,zh;q=0.9

Cookie:iex-jsessionid=wfplpuvbbbkq76xpcr9txrb4vok0eeso; hm_lvt_4320d635415dcfd7831a11fa64aec173=1526347940; hm_lpvt_4320d635415dcfd7831a11fa64aec173=1526347940; Spring_security_remember_me_cookie=ue9ksdfgmnvss291s2z2v1k5c1daqt09ojzxwfvduuhovmr2utdswnrfrnlzz0e9pq

5. Replace the captured http request header information with a dictionary using the sublime text tool

Select content substitution by using regular expressions

content Fill in: ^ (. *): \s (. *) $ (need to process the header file content into a dictionary, there is a space in the middle, so add a \s)

Replace with fill in: "\1": "\2",

The point is changed, and the processing is completed and becomes as follows:

"Host": "Demo.bxcker.com",

"Connection": "Keep-alive",

"Pragma": "No-cache",

"Cache-control": "No-cache",

"Accept": "*/*",

"X-requested-with": "XMLHttpRequest",

"User-agent": "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/66.0.3359.139 safari/537.36 ",

"Referer": "http://demo.bxcker.com/",

"Accept-encoding": "gzip, deflate",

"Accept-language": "zh-cn,zh;q=0.9",

"Cookie": "Iex-jsessionid=wfplpuvbbbkq76xpcr9txrb4vok0eeso; hm_lvt_4320d635415dcfd7831a11fa64aec173=1526347940; hm_lpvt_4320d635415dcfd7831a11fa64aec173=1526347940; Spring_security_remember_me_cookie=ue9ksdfgmnvss291s2z2v1k5c1daqt09ojzxwfvduuhovmr2utdswnrfrnlzz0e9pq ",

6. copy directly to Python -defined header information variable headers={} after processing is complete

7. Organize headers information (remove some useless, keep some important)

"Connection": "Keep-alive",

"X-requested-with": "XMLHttpRequest",

"User-agent": "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/66.0.3359.139 safari/537.36 ",

"Referer": "http://demo.bxcker.com/",

"Cookie": "Iex-jsessionid=wfplpuvbbbkq76xpcr9txrb4vok0eeso; hm_lvt_4320d635415dcfd7831a11fa64aec173=1526347940; hm_lpvt_4320d635415dcfd7831a11fa64aec173=1526347940; Spring_security_remember_me_cookie=ue9ksdfgmnvss291s2z2v1k5c1daqt09ojzxwfvduuhovmr2utdswnrfrnlzz0e9pq ",

8. Write the complete program.

From Urllib Import Parse

Import Urllib.request

url = "Http://demo.bxcker.com/customer/index.shtml"

headers={

"Host": "Demo.bxcker.com",

"Connection": "Keep-alive",

"Pragma": "No-cache",

"Cache-control": "No-cache",

"Accept": "*/*",

"X-requested-with": "XMLHttpRequest",

"User-agent": "mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.36 (khtml, like Gecko) chrome/66.0.3359.139 safari/537.36 ",

"Referer": "http://demo.bxcker.com/",

"Accept-encoding": "gzip, deflate",

"Accept-language": "zh-cn,zh;q=0.9",

"Cookie": "Iex-jsessionid=wfplpuvbbbkq76xpcr9txrb4vok0eeso; hm_lvt_4320d635415dcfd7831a11fa64aec173=1526347940; hm_lpvt_4320d635415dcfd7831a11fa64aec173=1526347940; Spring_security_remember_me_cookie=ue9ksdfgmnvss291s2z2v1k5c1daqt09ojzxwfvduuhovmr2utdswnrfrnlzz0e9pq ",

}

# URL as a parameter to the request () method, construct and return a request object

Request = urllib.request.Request (URL)

# Request object as a parameter to the Urlopen () method, send to the server and receive a response

Response = Urllib.request.urlopen (Request)

html = Response.read (). Decode (' Utf-8 ')

Print (HTML)

Operation Result:

You can see the information in the customer list, prove to be successful through a cookie, and then jump to the customer management module to obtain customer information.

Python3 using cookies to impersonate a login

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.