Python implements analog login "Go"

Source: Internet
Author: User

Original URL: http://www.blogjava.net/hongqiang/archive/2012/08/01/384552.htmlThis article mainly uses Python to realize the simulation login to the website. Use Python to implement the login process by constructing the post data yourself.  when you want to simulate login to a website, first of all to understand the site's login processing details (what kind of data sent, to whom, etc...) )。 I'm using Httpfox to crawl HTTP packets to analyze the site's login process. At the same time, we will analyze the data structure and header of the captured post packet, and construct its own post data and header according to the data structure and Heander submitted.  at the end of the analysis, we construct our own HTTP packet and send it to the specified URL. We use the API provided by several modules, such as URLLIB2, to enable the sending and receiving of request requests. Most websites require cookies when they sign in, so we must also set up cookie processors to guarantee cookies.  

Specific codes and explanations are as follows

    #!/usr/bin/python Import htmlparser Import urlparse import urllib import urllib2 Import Cookielib Import string Import re #登录的主页面 hosturl = ' ****** '//fill #post数据接收和处理的页面 yourself (we're going to this The page sends our constructed post data) PostURL = ' ****** '//parsed from the packet, the URL that handles the POST request #设置一个cookie处理器, it is responsible for downloading the cookie from the server to the local, and when sending the request with the local The cookie CJ = Cookielib. Lwpcookiejar () Cookie_support = Urllib2. Httpcookieprocessor (CJ) opener = Urllib2.build_opener (Cookie_support, Urllib2.      HttpHandler) Urllib2.install_opener (opener) #打开登录主页面 (his purpose is to download cookies from the page so that we have a cookie when we send the post data, otherwise the delivery is unsuccessful) h = Urllib2.urlopen (Hosturl) #构造header, the general header must contain at least two items.      These two items are analyzed from the bag caught. headers = {' user-agent ': ' mozilla/5.0 (Windows NT 6.1; WOW64;      rv:14.0) gecko/20100101 firefox/14.0.1 ', ' Referer ': ' ****** '} #构造Post数据, he is also from the grasp of the big bag analysis. PostData = {' op ': ' Dmlogin ', ' F ': ' st ', ' user ': '',//Your username ' pass ': ' ****** ',//Your password, the password may be clear text transmission may also be ciphertext, if ciphertext needs to call the corresponding encryption algorithm encryption ' RMBR ': '            True ',//unique data, different sites may differ ' tmp ': ' 0.7306424454308195 '//unique data, different sites may be different} #需要给Post数据编码 postdata = Urllib.urlencode (postdata) #通过urllib2提供的request方法来向指定Url发送我们构造的数据 and complete the login process R Equest = Urllib2. Request (PostURL, postdata, headers) Print Request response = Urllib2.urlopen (request) Text = Response.read (   ) Print text

Python implements analog login "Go"

Related Article

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.