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 ...