如何翻译脚本i python 2,7

时间:2015-04-10 20:16:11

标签: python

此脚本适用于Python34 如何在Python27中翻译?

import urllib.request, re, urllib.parse
from base64 import b64encode
import codecs
import time

def send_cap(key, fn):
    print ('1')
    data=open(fn, 'rb')
    print ('2')
    s = b64encode(data.read())
    print ('3')
    image = s.decode('latin-1')
    print ('4')
    params = urllib.parse.urlencode({'method': 'base64', 'key': key, 'body':image, 'ext':'jpg'})
    print ('5')
    bindata=params.encode('ascii')
    f = urllib.request.urlopen("http://antigate.com/in.php", bindata)#params)
    print ('6')
    res = str(f.read().decode())
    print(res)
    cap_id = re.search('\d+',res)
    if cap_id == None:
        return False
    else:
        idc = cap_id.group(0)
        return idc

def get_cap_text(key, cap_id):


    time.sleep(5)

    res_url= 'http://antigate.com/res.php'
    res_url+= "?" + urllib.parse.urlencode({'key': key, 'action': 'get', 'id': cap_id})
    flag=False
    while flag==False:
        res= urllib.request.urlopen(res_url).read()
        res = res.decode()
        if res == 'CAPCHA_NOT_READY':
            time.sleep(1)
        else:
            flag=True
    res= re.split('[\W]+',res)
    if len(res[0]) == 2:
        return res[1]
    else:
        return ('ERROR', res[0])

res = send_cap('eeeee','capcha2.jpg')
print(res)
text= get_cap_text('eeeee', res)
print (text)

1 个答案:

答案 0 :(得分:1)

如果这在Python 3中有效,那么您应该更改包含urllib的几行以进行转换:

import urllib, re # first line of imports

urllib.urlopen # no urllib.request
urllib.urlencode # no urllib.parse