Skip to content

Commit

Permalink
Add timeout for 'synccheck' and 'webwxsync'
Browse files Browse the repository at this point in the history
to avoid:

1. get 1101 error frequently
2. won't logout even if network broken
  • Loading branch information
youfou authored and littlecodersh committed May 9, 2017
1 parent e64ed7b commit bb853df
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 4 deletions.
5 changes: 4 additions & 1 deletion itchat/components/hotreload.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,10 @@ def load_login_status(self, fileDir,
self.loginInfo['User'].core = self
self.s.cookies = requests.utils.cookiejar_from_dict(j['cookies'])
self.storageClass.loads(j['storage'])
msgList, contactList = self.get_msg()
try:
msgList, contactList = self.get_msg()
except:
msgList = contactList = None
if (msgList or contactList) is None:
self.logout()
load_last_login_status(self.s, j['cookies'])
Expand Down
4 changes: 2 additions & 2 deletions itchat/components/login.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def sync_check(self):
'synckey' : self.loginInfo['synckey'],
'_' : int(time.time() * 1000),}
headers = { 'User-Agent' : config.USER_AGENT }
r = self.s.get(url, params=params, headers=headers)
r = self.s.get(url, params=params, headers=headers, timeout=config.TIMEOUT)
regx = r'window.synccheck={retcode:"(\d+)",selector:"(\d+)"}'
pm = re.search(regx, r.text)
if pm is None or pm.group(1) != '0':
Expand All @@ -301,7 +301,7 @@ def get_msg(self):
headers = {
'ContentType': 'application/json; charset=UTF-8',
'User-Agent' : config.USER_AGENT }
r = self.s.post(url, data=json.dumps(data), headers=headers)
r = self.s.post(url, data=json.dumps(data), headers=headers, timeout=config.TIMEOUT)
dic = json.loads(r.content.decode('utf-8', 'replace'))
if dic['BaseResponse']['Ret'] != 0: return None, None
self.loginInfo['SyncKey'] = dic['SyncCheckKey']
Expand Down
3 changes: 2 additions & 1 deletion itchat/config.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import os, platform

VERSION = '1.3.5'
VERSION = '1.3.6'
BASE_URL = 'https://login.weixin.qq.com'
OS = platform.system() #Windows, Linux, Darwin
DIR = os.getcwd()
DEFAULT_QR = 'QR.png'
TIMEOUT = (10, 30)

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 comments on commit bb853df

Please sign in to comment.