Use Python to write the ticket program

Source: Internet
Author: User
Tags ticket

The following restrictions are generally needed to swipe a ticket:

1. Verification Code Identification

2. The same IP cannot be continuously voted

Solutions

1, with Tesseract tool, link in this https://code.google.com/p/tesseract-ocr/(everyone still can't add HTTPS link)

2, the use of agents, the domestic proxy server can be found from here http://cn-proxy.com/

Program language of course with Python

The browser voting process is as follows

1, send a request to the server, the server returns the verification code and form

2. Fill out the form and send it to the server

You can use the Firefox+httpfox plugin to view the entire event process and send the requested post and get data formats

Use Python's urllib2 library for this process

1. Request a verification code from the server

can be used

+begin python

Import Urllib

Urllib.urlretrieve (Imgurl,imgfile) #imgurl可以从页面源代码找到

+end

However, the verification code will be regenerated every time the server is requested, so the verification code of the request path will be incorrect even if it is identified and then post in. Use cookies when resolving

+begin python

Import Cookie

Import Urllib2

Cookiejar=cookielib. Mozillacookiejar (Cookiefilename)

Opener=urllib2.build_opener (URLLIB2. Httpcookieprocessor (Cookiejar))

Response=opener.open (Imgurl)

+end

If you need to use a proxy server, you can write this

+begin python

Import Cookie

Import Urllib2

Proxy_line= ' 127.0.0.1:8087 '

Cookiefilename= ' Cookie.txt '

Cookiejar=cookielib. Mozillacookiejar (Cookiefilename)

Opener=urllib2.build_opener (URLLIB2. Proxyhandler ({' http ':p roxy_line}), Urllib2. Httpcookieprocessor (Cookiefilejar))

Response=opener.open (imgurl,timeout=2) #设置超时时间

+end

This is the local 8087 port with a cookie authentication access server, the next step to identify the verification code, still with a cookie post into the server.

2. Save verification Code to local

The above response is the return of the verification code binary stream, written to the file with the following code

+begin python

Content=response.read ()

Fp=file (Imgfile, ' WB ') #将二进制图片保存

Fp.write (content)

Fp.close ()

+end

3. Verification Code Identification

Verification code identification with Tesseract, because the tesseract does not provide a Python interface, here with the system call external command

Usage is

+begin python

Import OS
Imgfile= ' img.jpg '

Out

Brush tickets with Python
#-*-Coding:utf-8-*-import osimport urllibimport urllib2import stringimport socksimport httplib2import cookielibimport Timeimport randomtes= ' Tesseract.exe ' filepath= './' imgurl= ' http://example.com/vote/img.jsp ' myurl= '/http Example.com/vote "voteinfoid= ' xxxxxxxx ' headers={' user-agent ': ' mozilla/5.0 (Windows NT 6.1; WOW64; rv:25.0) gecko/20100101 firefox/25.0 '}#,# ' Cookie ': ' 324e661de12427bd71ce63df436a80d1 '}imgfile=filepath + '/img.jpg ' Outfile=filepath + '/out ' proxy_file=filepath+ '/proxy.txt ' user_file=filepath+ '/user2.txt ' CookieFilename=filepath + '/cookies.txt ' #myproxy_line = ' 211.142.236.137:80 ' #cookieJarFileLWP =cookielib. Lwpcookiejar (Cookiefilename) cookiefilejar=cookielib. Filecookiejar (cookiefilename) #opener =urllib2.build_opener (urllib2. Proxyhandler ({' http ': Myproxy_line}), Urllib2. Httpcookieprocessor (Cookiemozillajar)) Opener=urllib2.build_opener (urllib2. Httpcookieprocessor (Cookiefilejar)) F_user=open (user_file) f_proxy=open (proxy_file) Proxy_all=f_proxy.read (). Split (' \ n ') F_user=open (USer_file) User_all=f_user.read (). Split (' \ n ') count=1cookiefilejar=cookielib. Filecookiejar (cookiefilename) #使用cookie登陆while true:i=random.randint (1,100) j=random.randint (1,60) #print User_al                L.length () User_line=user_all[i] myproxy_line=proxy_all[j] for myproxy_line in [Myproxy_line]: #f_proxy: #使用代理和cookie Opener=urllib2.build_opener (urllib2. Proxyhandler ({' http ': Myproxy_line}), Urllib2. Httpcookieprocessor (Cookiefilejar)) Try:response2=opener.open (imgurl,timeout=1) #返回二进制图片 except E Xception,e:print ' I can not connect the server,try again ' continue Content=response2.rea  D () Fp=file (Imgfile, ' WB ') #将二进制图片保存 fp.write (content) Fp.close () outcmd= "%s%s%s-l eng digits        -PSM 7 "% (tes,imgfile,outfile) print ' I begin to recognize the CAPTCHA code: ' Os.system (Outcmd) code_file=open (filepath+ '/out.txt ') mycode_line=code_file.readline () codE_file.close () If Len (mycode_line) <=3:print ' I guess the CAPTCHA code is%s,but I think it\ ' s error. '% (Mycode_line) continue mycode=mycode_line[0:4] print ' I guess the CAPTCHA code is%s '% (MyCo         de) mylist=user_line.split ('----') proxy_list=myproxy_line.split (': ') myid=mylist[0]#.decode (' Utf-8 ')              Myname=mylist[1]#.decode (' Utf-8 ') mycomm=mylist[2]#.decode (' utf-8 ') data={' method ': ' Vote ',              ' Voteinfoid ': voteinfoid, ' forward ': ' * * * ', ' info1 ': myID, ' Info2 ': myname,        ' Info3 ': Mycomm, ' Inputcode ': mycode, ' submit ': ' OK '} print ' Now I begin to vote ... '         print ' The user is%s '% (myID) print ' The name of '%s '% (myname) print ' The comment is%s '% (Mycomm) Post_data=urllib.urlencode (data) Try:response=opener.open (myurl,post_data) except except Ion,e:print' I can\ ' t connect the server, so vote is failure ' continue content=response.read () #img_req =opener.op En (Imgurl) #cookieFileJar. Save (Cookiefilename) #req =opener.open (myurl,post_data) #req =urllib2.  Request (myurl,data=post_data,headers=headers) fp=file (' test.html ', ' W ') print ' I put the received HTML to the        File test.html ' fp.write (content) fp.close () #outcmd = "%s%s%s"% (tes,imgfile,outfile) #count +=1 #print Cookiefilejar

Log instructions on everyone

Http://blog.renren.com/blog/246819165/919178553?bfrom=01020650100&ref=hotnewsfeed&sfet=602&fin=0 &fid=24374271878&ff_id=246819165&platform=0&expose_time=1387116330

Use Python to write the ticket program

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.