The process of python obtaining WeChat Mini Programs's openid through code is very slow.

brief description of the problem

django is the backend. The process of getting openid < / openid > and session_key from the code sent by wx.login () to the Wechat server is too long ( 15s or so), but it is faster to access it directly through the browser.

Development environment

Ubuntu18.04 LTS , Django2.0.8 , Python3.6.5

related codes

here is a simple program I built to get openid:

class WeChatApi():

    def __init__(self, appid, secret):
        self.appid = appid
        self.secret = secret

    def get_openid_and_session_key(self, code):
        import time
        start = time.perf_counter()
        parmas = {
            "appid": self.appid,
            "secret": self.secret,
            "js_code": code,
            "grant_type": "authorization_code"
        }

        url = "https://api.weixin.qq.com/sns/jscode2session"
        r = requests.get(url, params=parmas)
        openid = r.json().get("openid", "")
        end = time.perf_counter()
        print("openid:", end-start, "")
        return openid

result

result: the program returns normally, but it takes too long, about 15s (has been tested repeatedly). When testing with postman , it takes about 15 seconds. But paste the interface url directly into the browser and you will get the result quickly.

doubts and puzzles

What is the reason for this phenomenon? How to solve it?

Jul.19,2021

this problem should be caused by the network environment of your machine. Whether you use a proxy or not, you can put the code in other network environment (such as CVM) to test


network environment. I just tried it in seconds.

Menu