`itchat`配置代理

config.py配置

首先,先找到itchat安装的目录,然后定位到config.py文件:

import os, platform

VERSION = '1.3.10'
BASE_URL = 'https://login.weixin.qq.com'
OS = platform.system() # Windows, Linux, 
DarwinDIR = os.getcwd()
DEFAULT_QR = 'QR.png'
TIMEOUT = (10, 60)
USER_AGENT = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.71 Safari/537.36'

在配置文件中添加代理设置

PROXIES = {'http':'http://公司代理IP地址:公司代理http端口号','https':'https://公司代理IP地址:公司代理https端口号'}

注:

这里的PROXIES 设置的是requests库中的session.py文件中的Session类实例化对象是属性中的代理,其中部分源码如下:

#: Dictionary mapping protocol or protocol and host to the URL of the proxy
#: (e.g. {'http': 'foo.bar:3128', 'http://host.name': 'foo.bar:4012'}) to
#: be used on each :class:`Request <Request>`.
self.proxies = {}

微信是用的是https协议,所以https也需配置

core.py配置

core.py文件中的self.s = requests.Session()下面插入self.s.proxies.update(config.PROXIES)

utlis.py配置

找到def test_connect(retryTime=5)函数,把函数里的r = requests.get(config.BASE_URL)改成r = requests.get(config.BASE_URL,proxies = config.PROXIES),然后运行主程序就ok了!

本文参考了简书文章:如何让itchat支持代理

链接:https://www.jianshu.com/p/daa5c41dbe3f

posted @ 2019-06-10 14:22  咕咚!  阅读(590)  评论(0编辑  收藏  举报