代码改变世界

为朋友写的一个投票功能的提交代码

2010-05-08 17:19  ubunoon  阅读(1444)  评论(4编辑  收藏  举报

最近一直研究验证码等图形处理,刚好碰到一朋友说要想投票一下,就写了这个投票功能的python代码:

 

 

#! /usr/bin/env python
#coding=utf-8

# 实现了最基础的投票功能

import capatcha
import urllib2
import urllib
import os
import datetime
import time
import random
import cookielib

def getsecond():
    now = datetime.datetime.now()
    return time.mktime( now.timetuple() )

def randvalue():
    v = []
    for i in range( random.randint(5,20) ):
        v.append( str(i) )
    return ''.join(v)

def post():
    # 制作cookie
    cookie = cookielib.CookieJar()
    opener = urllib2.build_opener( urllib2.HTTPCookieProcessor(cookie) )
    
    url = 'http://124.225.113.132/voteserver/JGYJ_HuaD/sms.ASP?ID=343'    
    request = urllib2.Request( url )
    request.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0 Windows NT 5.2; .NET CLR 1.1.4322)')
    response = opener.open( request )
    capatcha_url = 'http://124.225.113.132/voteserver/JGYJ_HuaD/getcode.asp?t=%s' %str(getsecond())    
    filename = os.path.join( os.getcwd() , 'temp.bmp' )
    urllib.urlretrieve(capatcha_url, filename)
    value = capatcha.picture_reg.reg( filename )  # 验证码识别
    
    print value
    
    url = 'http://124.225.113.132/voteserver/JGYJ_HuaD/sms.ASP?verifycode=%s&W02_Name=%s&W04_Phone=%s&W03_Message=%s' %( value, randvalue(), randvalue(),randvalue())
    url += '&Submit2=%CC%E1%BD%BB&ID=343'
    print url
    request = urllib2.Request( url )
    request.add_header('User-Agent', 'Mozilla/4.0 (compatible; MSIE 7.0 Windows NT 5.2; .NET CLR 1.1.4322)')
    #request.set_proxy( '74.115.1.10:80', 'http' )
    #opener = urllib2.build_opener()
    response = opener.open( request )
    
    data = response.read()
    print data
    response.close()


def main():
    post()
    
if __name__ == '__main__':
    main()

代码写的很粗糙,不过也简单。

 

挂个小广告,阿里巴巴珍珠饰品

http://zjyuezz.cn.alibaba.com/

 

blog来自:

http://www.cnblogs.com/ubunoon/archive/2010/05/08/1730558.html

 

后记:

看了吉日的回复,也看了他很多的blog,决定将这个页面放到首页上去。