本地变量' urllib'在分配之前引用,即使我导入它

时间:2015-02-19 00:59:09

标签: python python-3.x urllib

使用python 3.4,我无法找出问题。我看过其他网站&人们遇到同样问题的线索,但他们的解决方案对我没有帮助。

import urllib,urllib.parse,urllib.request,random

try:
    if len(args)>20: room.message("Your username can't be more than 20 letters!")
    else:
        if "<i><font color=#000000>{0}</font></i> is not (yet) a registered Chatango name!<br/><br/>".format(args) in str(urllib.request.urlopen("http://{0}.chatango.com/".format(args)).read().decode()):
            try:
                rand = (' '.join(random.choice(string.ascii_letters+string.digits+'~!@#$%^&*()_+{}|:"<>?') for i in range(random.randint(8,20)))).replace(' ','')
                dat = urllib.parse.urlencode({
                    "email": ("usr"+str(random.randint(100,10000))+"@duubz.bot"),
                    "login": args,
                    "password": rand, "password_confirm": rand,
                    "checkerrors": "yes"
                    }).encode()
                resp = urllib.request.urlopen("http://chatango.com/signupdir", dat)
                headers = resp.headers
                room.message('Private messaging you your password for {0}, {1}.'.format(args,user.name.capitalize()))
                self.pm.message(ch.User(user.name), 'Your password for the account {0} is: {1}'.format(args,rand))
            except Exception as e: room.message( str(e) )
        else: room.message("{0} is already a username.".format(args.upper()))
except Exception as e: room.message( str(e))

1 个答案:

答案 0 :(得分:0)

也许试试:

from urllib import (parse, request)

然后使用:

resp = request.urlopen("http://chatango.com/signupdir", dat)

并修复所有这些缩进!