littlecodersh / ItChat

A complete and graceful API for Wechat. 微信个人号接口、微信机器人及命令行微信,三十行即可自定义个人号机器人。
http://itchat.readthedocs.io
MIT License
25.52k stars 5.63k forks source link

发送图片显示 [Errno 111] Connection refused #313

Closed colorfulshark closed 7 years ago

colorfulshark commented 7 years ago

代码:

#coding=utf8
import itchat
# tuling plugin can be get here:
# https://github.com/littlecodersh/EasierLife/tree/master/Plugins/Tuling
#from tuling import get_response

@itchat.msg_register('Text')
def text_reply(msg):
    content = msg['Text']
    if(content == 'img'):
        itchat.send('@img@temp.jpg')
    if u'作者' in msg['Text'] or u'主人' in msg['Text']:
        return u'你可以在这里了解他:https://github.com/littlecodersh'
    elif u'源代码' in msg['Text'] or u'获取文件' in msg['Text']:
        itchat.send('@fil@main.py', msg['FromUserName'])
        return u'这就是现在机器人后台的代码,是不是很简单呢?'
    elif u'获取图片' in msg['Text']:
        itchat.send('@img@applaud.gif', msg['FromUserName']) # there should be a picture
    else:
        pass
        #return get_response(msg['Text']) or u'收到:' + msg['Text']

@itchat.msg_register(['Picture', 'Recording', 'Attachment', 'Video'])
def atta_reply(msg):
    msg['Text'](msg['FileName'])
    return ({ 'Picture': u'图片', 'Recording': u'录音',
        'Attachment': u'附件', 'Video': u'视频', }.get(msg['Type']) +
        u'已下载到本地') # download function is: msg['Text'](msg['FileName'])

@itchat.msg_register(['Map', 'Card', 'Note', 'Sharing'])
def mm_reply(msg):
    if msg['Type'] == 'Map':
        return u'收到位置分享'
    elif msg['Type'] == 'Sharing':
        return u'收到分享' + msg['Text']
    elif msg['Type'] == 'Note':
        return u'收到:' + msg['Text']
    elif msg['Type'] == 'Card':
        return u'收到好友信息:' + msg['Text']['Alias']

@itchat.msg_register('Text', isGroupChat = True)
def group_reply(msg):
    if msg['isAt']:
        pass
        #return u'@%s\u2005%s' % (msg['ActualNickName'],
            #get_response(msg['Text']) or u'收到:' + msg['Text'])

@itchat.msg_register('Friends')
def add_friend(msg):
    itchat.add_friend(**msg['Text'])
    itchat.send_msg(u'项目主页:github.com/littlecodersh/ItChat\n'
        + u'源代码  :回复源代码\n' + u'图片获取:回复获取图片\n'
        + u'欢迎Star我的项目关注更新!', msg['RecommendInfo']['UserName'])
itchat.auto_login()
itchat.run(debug = True)

错误日志:

Getting uuid of QR code.
Downloading QR code.
Please scan the QR code to log in.
Please press confirm on your phone.
Loading the contact, this may take a little while.

Login successfully as 王瑞
Start auto replying.
Uin fetched: @748e64eccc7f7b0847f7348fe1a45a69, wangrui1995225
Request to send a file(mediaId: None) to @748e64eccc7f7b0847f7348fe1a45a69: main.py
Request to upload a file: main.py
Traceback (most recent call last):
  File "/home/ryan/.local/lib/python2.7/site-packages/itchat/components/register.py", line 60, in configured_reply
    r = replyFn(msg)
  File "ipc-queue.py", line 15, in text_reply
    itchat.send('@fil@main.py', msg['FromUserName'])
  File "/home/ryan/.local/lib/python2.7/site-packages/itchat/components/messages.py", line 491, in send
    r = self.send_file(msg[5:], toUserName)
  File "/home/ryan/.local/lib/python2.7/site-packages/itchat/components/messages.py", line 377, in send_file
    r = self.upload_file(fileDir, preparedFile=preparedFile)
  File "/home/ryan/.local/lib/python2.7/site-packages/itchat/components/messages.py", line 330, in upload_file
    file_, chunk, chunks, uploadMediaRequest)
  File "/home/ryan/.local/lib/python2.7/site-packages/itchat/components/messages.py", line 361, in upload_chunk_file
    return requests.post(url, files=files, headers=headers)
  File "/home/ryan/.local/lib/python2.7/site-packages/requests/api.py", line 110, in post
    return request('post', url, data=data, json=json, **kwargs)
  File "/home/ryan/.local/lib/python2.7/site-packages/requests/api.py", line 56, in request
    return session.request(method=method, url=url, **kwargs)
  File "/home/ryan/.local/lib/python2.7/site-packages/requests/sessions.py", line 488, in request
    resp = self.send(prep, **send_kwargs)
  File "/home/ryan/.local/lib/python2.7/site-packages/requests/sessions.py", line 609, in send
    r = adapter.send(request, **kwargs)
  File "/home/ryan/.local/lib/python2.7/site-packages/requests/adapters.py", line 487, in send
    raise ConnectionError(e, request=request)
ConnectionError: HTTPSConnectionPool(host='file.wx2.qq.com', port=443): Max retries exceeded with url: /cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json (Caused by NewConnectionError('<requests.packages.urllib3.connection.VerifiedHTTPSConnection object at 0x7f1fc5a67a50>: Failed to establish a new connection: [Errno 111] Connection refused',))

itchat版本为:[1.3.5]

当发送“源代码”之后,理论上应该上传文件,但是上传始终报错,测试了littlecodersh,那个项目可以正常上传文件,网页版的微信可以正常登录,也可以正常发送文件

littlecodersh commented 7 years ago

@colorfulshark 你试试使用简单的程序测试:

import itchat

itchat.auto_login(True)
r = itchat.send('@fil@tesetfile.txt', 'filehelper')
print(r)