In this paper, we describe the method of Python script to realize the check-in function of shrimp net. Share to everyone for your reference, as follows:
Overview
This script completes the automatic log in the shrimp net, the sign-in function.
Roughly use Urllib, URLLIB2, cookielib these modules. The fact is to use Python to implement the post data to the specified URL.
As for how I know what data the browser posts to the server when I log in and check in in the browser, you can see it in a powerful chrome:f12->network. Some server login success will allow the client browser to jump or immediately refresh the page and so on, will log on to the server post data brush is not lost, then you need to use the powerful capture package software--wireshark to analyze. For example, with chrome:
Then you can write, construct the request headers and the data to post (that is, the form data in chrome), ID and PWD are clear text transmission, hehe.
Code
Here's the code:
#-*-coding:cp936-*-"" Created on 2012-11-15@author:liushuai "Import Urllib, Urllib2, Cookielib, Sysclass Loginxiam I:login_header = {' user-agent ': ' mozilla/5.0 (Windows NT 6.1) applewebkit/537.4 (khtml, like Gecko) chrome/22.0.1229.79 S afari/537.4 '} signin_header = {' user-agent ': ' mozilla/5.0 (Windows NT 6.1) applewebkit/537.4 (khtml, like Gecko) CHROME/22 .0.1229.79 safari/537.4 ', ' x-requested-with ': ' XMLHttpRequest ', ' content-length ': 0, ' Origin ': ' http://www.xiami.com ' , ' Referer ': ' http://www.xiami.com/'} email = ' Password = ' cookie = None cookiefile = './cookie.dat ' Def __init__ ( Self, email, pwd): self.email = Email Self.password = pwd Self.cookie = cookielib. Lwpcookiejar () opener = Urllib2.build_opener (urllib2. Httpcookieprocessor (Self.cookie)) Urllib2.install_opener (opener) def login (self): PostData = {' Email ': self.email, ' Password ': Self.password, ' done ': ' http://www.xiami.com ', ' Submit ': '%e7%99%bb+%e5%bd%95 '} postdata = Urllib.urlencode (PostData) print ' logining ... ' req = urllib2. Request (url= ' Http://www.xiami.com/member/login ', data=postdata, headers=self.login_header) result = Urllib2.urlopen (req). Read () Self.cookie.save (self.cookiefile) result = str (result). Decode (' Utf-8 '). Encode (' GBK ') if ' Email or password is wrong Error ' in Result:print ' login failed due to Email or Password error ... ' sys.exit () else:print ' login succ essfully! ' def signIn (self): PostData = {} PostData = Urllib.urlencode (postdata) print ' signing ... ' req = urllib2. Request (url= ' Http://www.xiami.com/task/signin ', data=postdata, headers=self.signin_header) result = Urllib2.urlopen (req). read () result = str (result). Decode (' Utf-8 '). Encode (' GBK ') self.cookie.save (self.cookiefile) Try:resul t = Int (result) except Valueerror:print ' signing failed ... ' Sys.exit () Except:print ' signing failed Due to unknown reasons ... ' sys.exit () print ' signing successfully! ' Print Self.email, ' have signed ', result, ' days continuously ... ' if __name__ = = ' __main__ ': User = Loginxiami (' Your login mailbox ', ' Your password ') user.login () user.sign In ()
Then run the script to log in and sign in.
Postscript
Some discuz! forums are said to prevent users from using the Web to post data to the server, the server generates a value called Formhash each time a user logs on successfully, and returns to the client as a hidden domain of the form (the service side also has a save).
When the client posts data to the server, this value is used as a form of data "unnoticed" (because it is a hidden field of the form) to post the past, the service side received a POST request from the client, and the service side of the Formhash value for comparison, will be able to know whether the data is post via a browser page.
But if it is really to prevent this, is this really useful? I tried, just use simple regular analysis login successfully returned HTML, find Formhash value, and then use the above method and theory post past can, successfully completed the check-in function.
Anyway, the script "pretends" to complete the process of using a browser to sign in and check in.
attached : As the site may be revised or upgraded, then post data to the server may change, then the code should be adjusted accordingly, the code is only for research and learning reference.
More readers interested in Python related content can view this site topic: "Introduction to Python and Advanced Classic Tutorial" and "Python file and directory Operations Tips Summary"
I hope this article is helpful for Python program design.