Skip to content

Instantly share code, notes, and snippets.

@zhangyoufu
Last active August 29, 2015 13:56
Show Gist options
  • Save zhangyoufu/8839650 to your computer and use it in GitHub Desktop.
Save zhangyoufu/8839650 to your computer and use it in GitHub Desktop.
WeChat Web Login Demo (Need QR Scan)
import requests
import re
default_host = 'wx.qq.com'
# default_host = 'web.weixin.qq.com'
# default_host = 'web.wechat.com'
# default_host = 'web2.wechat.com'
# default_host = 'web.wechatapp.com'
# default_lang = 'en_US'
default_lang = 'zh_CN'
# default_lang = 'zh_TW'
max_retry = 3
max_redirect = 3
class UnknownServer(Exception): pass
def refresh_domain():
global login_host, cookie_domain, qrcode_host
if host.endswith('.qq.com'):
login_host = 'login.weixin.qq.com'
elif host.endswith('.wechat.com'):
login_host = 'login.wechat.com'
elif host.endswith('.wechatapp.com'):
login_host = 'login.wechatapp.com'
else:
raise UnknownServer
cookie_domain = '.'.join( ['']+login_host.rsplit('.', 2)[-2:] )
qrcode_host = 'login.weixin.qq.com'
def get_uuid():
query = dict(
appid = 'wx782c26e4c19acffb',
redirect_uri = 'https://'+host+'/cgi-bin/mmwebwx-bin/webwxnewloginpage',
)
data = requests.get( 'https://'+login_host+'/jslogin', params=query ).content
uuid = re.search( 'uuid = "([0-9a-f]+)"', data ).group(1)
print 'uuid:', uuid
return uuid
def show_qrcode(uuid):
import qrcode
url = 'https://'+qrcode_host+'/l/'+uuid
qrcode.make( url ).show()
print 'Please scan and confirm on your device'
translate_code = {
200: 'Confirmed',
201: 'Scanned',
400: 'Error',
408: 'Timeout',
500: 'Error',
}
class UUIDExpire(Exception): pass
def poll_confirm(uuid):
print 'Polling...'
code = None
while code not in [400,500]:
data = requests.get( 'https://'+login_host+'/cgi-bin/mmwebwx-bin/login?uuid='+uuid ).content
code = int( re.search( r'code=(\d+)', data ).group(1) )
print code, translate_code[code]
if code == 200:
return re.search( 'window.redirect_uri="(.*?)"', data ).group(1)
else:
raise UUIDExpire
class TooManyRedirect(Exception): pass
def get_cookies(url):
for i in xrange( max_redirect ):
print '[%d]'%i, url
resp = requests.get( url, allow_redirects=False )
print 'HTTP %d, Length: %d' % ( resp.status_code, len(resp.content) )
if len(resp.cookies) > 1: break
url = re.search( 'window.location.href="(.*?)"', resp.content ).group(1)
else:
raise TooManyRedirect
host = re.search( '://([^/]+)', url ).group(1)
wxuin = resp.cookies['wxuin']
wxsid = resp.cookies['wxsid']
print 'wxuin:', wxuin
print 'wxsid:', wxsid
return host, wxuin, wxsid
def open_browser( wxuin, wxsid, lang=default_lang ):
try:
from selenium import webdriver
driver = webdriver.Chrome()
# need a document before add_cookie
driver.get( 'https://'+login_host+'/cgi-bin/mmwebwx-bin/login' )
driver.add_cookie( dict( name='mm_lang', value=lang, domain=cookie_domain ))
driver.add_cookie( dict( name='wxuin', value=wxuin, domain=cookie_domain ))
driver.add_cookie( dict( name='wxsid', value=wxsid, domain=cookie_domain ))
# navigate to home page
driver.get( 'https://'+host+'/' )
except:
print 'Cannot open in browser'
def main():
global host
for i in xrange( max_retry ):
try:
host = default_host
refresh_domain()
uuid = get_uuid()
show_qrcode(uuid)
url = poll_confirm(uuid)
host, wxuin, wxsid = get_cookies(url)
refresh_domain()
open_browser( wxuin, wxsid )
return
except Exception as e:
print e
print
else:
print 'max retry exceed'
return
if __name__ == '__main__':
main()
@zhangyoufu
Copy link
Author

Revision 2 support redirection between domains

e.g: logging in an US account at wx.qq.com will be redirected to web2.wechat.com (203.205.161.172, Canada server)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment