Getting Started with Python: Analog login (i) Urllib__python

Source: Internet
Author: User

We often need to write a script to test the web in Python, but now many sites need to log in to do the next step, so Python analog login is important in our day-to-day operations, and I'd like to give you a brief introduction of how Python implements Web logins (not to spray, Great God Bypass):
First, the website analysis
Analyze the landing page for the target web, mainly to get the format and parameter names that are passed to the server at logon (there are many tools, such as Firefox firebug plug-ins, etc.)
1, open the Analysis tool, and select "Network" "Keep"

2, click to login, and fill in the user name, password and other information

3, find the login URL (usually post, only very rubbish site will use get way)

4. Get Login Parameters

5, the final obtained parameters

pasturl=&username1=%e7%94%a8%e6%88%b7%e5%90%8d%2f%e6%89%8b%e6%9c%ba%e5%8f%b7&loginname=11111& LoginPass1
=%e8%af%b7%e6%82%a8%e8%be%93%e5%85%a5%e5%af%86%e7%a0%81&loginpass=111111

Second, analog login code writing

Import urllib
import urllib2

values = {"LoginName": "11111", "Loginpass": "111111"}
data = Urllib.urlencode (values)
url = "Http://10.70.18.47:8080/user/login"

request = Urllib2. Request (Url,data)
response = Urllib2.urlopen (request)
print (Response.read ())

iii. Analog Login return results

["{\" id\ ": \" unlogin\ "," issuccess\ ": True,\" info\ ": \" \ "}", "{\" id\ ": \" pwlogin\ ", \" issuccess\ ": True,\" info\ ": \" Malformed password \ "}", "{\" pasturl\: Null,\ "issuccess\": True,\ "info\": \ "landing successful \"} "
[finished in 0.3s]

The result of this code execution is consistent with the result of the successful return of the login to the target Web site, indicating that the impersonation login code was successfully written ...

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.