14

python proxy.py の実行中にエラーが発生しました

$ python proxy.py 
INFO - [Sep 28 14:59:19] getting appids from goagent plus common appid pool!
Traceback (most recent call last):
  File "proxy.py", line 2210, in <module>
    main()
  File "proxy.py", line 2180, in main
    pre_start()
  File "proxy.py", line 2157, in pre_start
    common.set_appids(get_appids())
  File "proxy.py", line 94, in get_appids
    fly = bytes.maketrans(
AttributeError: type object 'str' has no attribute 'maketrans'

https://code.google.com/p/smartladder/の proxy.py ファイル、

def get_appids():
    fly = bytes.maketrans(
        b"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        b"nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
    )
    f = urllib.request.urlopen(url="http://lovejiani.com/v").read().translate(fly)
    d = base64.b64decode(f)
    e = str(d, encoding='ascii').split('\r\n')
    random.shuffle(e)
    return  e
4

1 に答える 1

18

Python 3 用に書かれたコードを Python 2 で実行しています。これは機能しません。

maketransbytes組み込み型のクラスメソッドですが、Python 3 のみです

# Python 3
>>> bytes
<class 'bytes'>
>>> bytes.maketrans
<built-in method maketrans of type object at 0x10aa6fe70>

Python 2 では、bytesは のエイリアスですstrが、その型にはそのメソッドがありません:

# Python 2.7
>>> bytes
<type 'str'>
>>> bytes.maketrans
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: type object 'str' has no attribute 'maketrans'

代わりに Python 3 でコードを実行するか、このプロジェクトのすべてのコードを Python 2 に変換してください。後者は、Python 2 と 3 がどのように異なるかについての深い知識を必要とし、おそらく主要な作業です。

Python 2 に変換された図の関数だけは、次のようになります。

import string
import urllib2
import base64
import random

def get_appids():
    fly = string.maketrans(
        "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
        "nopqrstuvwxyzabcdefghijklmNOPQRSTUVWXYZABCDEFGHIJKLM"
    )
    f = urllib2.urlopen("http://lovejiani.com/v").read().translate(fly)
    d = base64.b64decode(f)
    e = unicode(d, encoding='ascii').split(u'\r\n')
    random.shuffle(e)
    return e
于 2013-09-28T07:12:15.360 に答える