def __upload_file(self, fileDir, isPicture = False): if not tools.check_file(fileDir): return url = 'https://file%s.wx.qq.com/cgi-bin/mmwebwx-bin/webwxuploadmedia?f=json'%('2' if '2' in self.loginInfo['url'] else '') # save it on server fileSize = str(os.path.getsize(fileDir)) cookiesList = {name:data for name,data in self.s.cookies.items()} fileType = mimetypes.guess_type(fileDir)[0] or 'application/octet-stream' files = { 'id': (None, 'WU_FILE_0'), 'name': (None, os.path.basename(fileDir)), 'type': (None, fileType), 'lastModifiedDate': (None, time.strftime('%a %b %d %Y %H:%M:%S GMT+0800 (CST)')), 'size': (None, fileSize), 'mediatype': (None, 'pic' if isPicture else 'doc'), 'uploadmediarequest': (None, json.dumps({ 'BaseRequest': self.loginInfo['BaseRequest'], 'ClientMediaId': int(time.time()), 'TotalLen': fileSize, 'StartPos': 0, 'DataLen': fileSize, 'MediaType': 4, }, separators = (',', ':'))), 'webwx_data_ticket': (None, cookiesList['webwx_data_ticket']), 'pass_ticket': (None, 'undefined'), 'filename' : (os.path.basename(fileDir), open(fileDir, 'rb'), fileType), } headers = { 'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.80 Safari/537.36', } r = self.s.post(url, files = files, headers = headers) return json.loads(r.text)['MediaId']