Skip to content

Commit bb853df

Browse files
youfoulittlecodersh
authored andcommitted
Add timeout for 'synccheck' and 'webwxsync'
to avoid: 1. get 1101 error frequently 2. won't logout even if network broken
1 parent e64ed7b commit bb853df

File tree

3 files changed

+8
-4
lines changed

3 files changed

+8
-4
lines changed

itchat/components/hotreload.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,10 @@ def load_login_status(self, fileDir,
5555
self.loginInfo['User'].core = self
5656
self.s.cookies = requests.utils.cookiejar_from_dict(j['cookies'])
5757
self.storageClass.loads(j['storage'])
58-
msgList, contactList = self.get_msg()
58+
try:
59+
msgList, contactList = self.get_msg()
60+
except:
61+
msgList = contactList = None
5962
if (msgList or contactList) is None:
6063
self.logout()
6164
load_last_login_status(self.s, j['cookies'])

itchat/components/login.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def sync_check(self):
282282
'synckey' : self.loginInfo['synckey'],
283283
'_' : int(time.time() * 1000),}
284284
headers = { 'User-Agent' : config.USER_AGENT }
285-
r = self.s.get(url, params=params, headers=headers)
285+
r = self.s.get(url, params=params, headers=headers, timeout=config.TIMEOUT)
286286
regx = r'window.synccheck={retcode:"(\d+)",selector:"(\d+)"}'
287287
pm = re.search(regx, r.text)
288288
if pm is None or pm.group(1) != '0':
@@ -301,7 +301,7 @@ def get_msg(self):
301301
headers = {
302302
'ContentType': 'application/json; charset=UTF-8',
303303
'User-Agent' : config.USER_AGENT }
304-
r = self.s.post(url, data=json.dumps(data), headers=headers)
304+
r = self.s.post(url, data=json.dumps(data), headers=headers, timeout=config.TIMEOUT)
305305
dic = json.loads(r.content.decode('utf-8', 'replace'))
306306
if dic['BaseResponse']['Ret'] != 0: return None, None
307307
self.loginInfo['SyncKey'] = dic['SyncCheckKey']

itchat/config.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
import os, platform
22

3-
VERSION = '1.3.5'
3+
VERSION = '1.3.6'
44
BASE_URL = 'https://login.weixin.qq.com'
55
OS = platform.system() #Windows, Linux, Darwin
66
DIR = os.getcwd()
77
DEFAULT_QR = 'QR.png'
8+
TIMEOUT = (10, 30)
89

910
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'

0 commit comments

Comments
 (0)