時(shí)間:2023-05-21 14:48:01 | 來(lái)源:網(wǎng)站運(yùn)營(yíng)
時(shí)間:2023-05-21 14:48:01 來(lái)源:網(wǎng)站運(yùn)營(yíng)
Python 微信公眾號(hào)開(kāi)發(fā)從0開(kāi)始避坑指南: 作為一個(gè) Python 愛(ài)好者,不把 Python 代碼和微信結(jié)合起來(lái)玩一玩,只能說(shuō)是自絕于時(shí)代。但自從騰訊對(duì)基于網(wǎng)頁(yè)版微信的微信自動(dòng)化狠下殺手后,我們就只能把眼光投向微信公眾號(hào)了。sudo dnf install python3
pip 也會(huì)同時(shí)安裝,使用時(shí)記得加上3,像 python3,pip3 install 這樣。web.py
lxml python
requests
pillow
等等,也要記得裝好,公眾號(hào)開(kāi)發(fā)文檔中有提到。[root@xxxxxxxxx~]# python3 main.py 80
從開(kāi)發(fā)文檔中開(kāi)始抄代碼 main.py ,上傳至云服務(wù)器。在 Putty 中輸入上面的命令。其中的 “80” 指的是端口 80。執(zhí)行命令后,根據(jù)文檔指引,在瀏覽器打開(kāi)你的網(wǎng)址,可以看到:hello, this is handle view
確認(rèn)你的網(wǎng)站應(yīng)用搭建成功。 list = [token, timestamp, nonce] list.sort() sha1 = hashlib.sha1() map(sha1.update, list) # <-- 巨坑1號(hào) hashcode = sha1.hexdigest() print "handle/GET func: hashcode, signature: ", hashcode, signature if hashcode == signature: return echostr else: return ""
上述代碼中的這一句:map(sha1.update, list)
需要修改為: sha1.update(list[0].encode('utf-8')) sha1.update(list[1].encode('utf-8')) sha1.update(list[2].encode('utf-8'))
運(yùn)行:[root@xxxxxxxxx~]# python3 main.py 80
在公眾號(hào)的配置頁(yè)面點(diǎn)“提交”,然后點(diǎn)擊“啟動(dòng)”。IP (xxx.xxx.xxx.xxx) is not in the whitelist
這時(shí)把它提醒你的 IP 添加至白名單即可。[root@xxxxxxxxx~]# python3 main.py 80
啟動(dòng)你心愛(ài)的程序后,一旦 Putty 關(guān)閉,遠(yuǎn)程連接斷開(kāi)后,程序就會(huì)自動(dòng)停止。你說(shuō)這不是坑爹嗎?如果本地一直要開(kāi)著窗口,那還叫什么價(jià)值一百元的云計(jì)算?[root@xxxxxxxxx~]# nohup python3 main.py 80 &
運(yùn)行后,可以看到:appending output to nohup.out
表示你的程序已經(jīng)成功運(yùn)行在云服務(wù)器的后臺(tái)。如果日后你需要停止程序則需要使用:ps -aux
來(lái)查找到 python3 main.py 80 的 PID
,然后使用 kill PID 來(lái)刪除。關(guān)于 Linux 這一塊的操作,可以參考這篇文章來(lái)學(xué)習(xí)。request.post
來(lái)實(shí)現(xiàn)。具體的操作可以參考以下過(guò)程def get_id(img): #img is bytes-data _token = gt.get_token() _url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token=' + _token + '&type=image' #print('url=', _url) payload = { 'file':('upload.jpg', img, 'image/jpg') } m = MultipartEncoder(payload) headers = { 'Content-Type':m.content_type, 'other-keys':'other-values' } _id = requests.post(_url, headers=headers, data=m).json() print('post request returns:', _id) _id = _id['media_id'] #print(_id) return _id
最后讓我們來(lái)一個(gè)綜合應(yīng)用。【需求】用戶(hù)發(fā)送一張圖片給公眾號(hào),例如拍攝的一頁(yè)數(shù)學(xué)作業(yè),后臺(tái)通過(guò) OCR 處理后自動(dòng)批改,返回一張批改后的圖片。就是如上圖這樣(令人捉急的正確率)。
關(guān)鍵詞:指南,公眾
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
客戶(hù)&案例
營(yíng)銷(xiāo)資訊
關(guān)于我們
微信公眾號(hào)
版權(quán)所有? 億企邦 1997-2025 保留一切法律許可權(quán)利。