ItChat icon indicating copy to clipboard operation
ItChat copied to clipboard

centos7 nohup sys.stdin.encoding为None导致编码错误

Open xzjs opened this issue 10 years ago • 10 comments

我又来问问题了,现在程序我在我的服务器上直接python run.py没有问题,但是我想用nohup执行的时候,就出现了问题,nohup.out如下

File "run.py", line 49, in <module>
    itchat.auto_login()
  File "/var/www/html/ItChat/itchat/__init__.py", line 13, in auto_login
    __client.auto_login(enableCmdQR = enableCmdQR)
  File "/var/www/html/ItChat/itchat/client.py", line 58, in auto_login
    open_QR()
  File "/var/www/html/ItChat/itchat/client.py", line 50, in open_QR
    out.print_line('Getting uuid', True)
  File "/var/www/html/ItChat/itchat/out.py", line 11, in print_line
    sys.stdout.write(msg.encode(sys.stdin.encoding, 'replace'
TypeError: encode() argument 1 must be string, not None`

现在又不知道该如何解决了

xzjs avatar Jul 21 '16 07:07 xzjs

@xzjs 这是因为在nohup当中无法读取到系统命令行的默认字符集 把out.py的11-12行改为下面的内容就可以了:

sys.stdout.write(msg.encode(sys.stdin.encoding or 'utf8', 'replace'
    ).decode(sys.stdin.encoding or 'utf8', 'replace'))

我会在下一个版本中修复进去

littlecodersh avatar Jul 21 '16 07:07 littlecodersh

@littlecodersh pull了最新的代码,现在二维码都没法生成了,只有我一个这样么

Getting QR Code
Failed to get QR Code, please restart the program

xzjs avatar Jul 22 '16 05:07 xzjs

@xzjs 试试看把86-101行的异常输出出来,看下是什么问题:

def get_QR(self, uuid = None, enableCmdQR = False):
    try:
        if uuid == None: uuid = self.uuid
        url = '%s/qrcode/%s'%(BASE_URL, uuid)
        r = self.s.get(url, stream = True)
        QR_DIR = 'QR.jpg'
        with open(QR_DIR, 'wb') as f: f.write(r.content)
        if enableCmdQR:
            tools.print_cmd_qr(QR_DIR)
        else:
            tools.print_qr(QR_DIR)
        return True
    except:
        import traceback
        traceback.print_exc()
        return False

littlecodersh avatar Jul 22 '16 05:07 littlecodersh

我觉得是路径问题吧,可能当前路径没写权限

tempdban avatar Jul 22 '16 05:07 tempdban

@littlecodersh 抛出的异常时这个 `Getting QR CodeTraceback (most recent call last): File "/Users/xzjs/Desktop/CodeComplete/python/ItChat/itchat/client.py", line 96, in get_QR tools.print_qr(QR_DIR) File "/Users/xzjs/Desktop/CodeComplete/python/ItChat/itchat/tools.py", line 49, in print_qr

subprocess.call(['open', fileDir])

NameError: global name 'subprocess' is not defined`

xzjs avatar Jul 22 '16 06:07 xzjs

忘了说了,我的开发环境是mac,部署环境是centos 7

xzjs avatar Jul 22 '16 06:07 xzjs

@xzjs 临时解决方案: 在/Users/xzjs/Desktop/CodeComplete/python/ItChat/itchat/tools.py 最上面加一行: import subprocess

tempdban avatar Jul 22 '16 07:07 tempdban

@tempdban 解决了,太感谢了

xzjs avatar Jul 22 '16 08:07 xzjs

坐等老大修复

tempdban avatar Jul 22 '16 08:07 tempdban

@tempdban 都是码代码的兄弟 :grin:

littlecodersh avatar Jul 22 '16 09:07 littlecodersh