将wxpy的登录二维码放到网页上登录
作者:互联网
from flask import Flask, Response
from flask.views import MethodView
from threading import Thread
from time import sleep
from wxpy import Bot
app = Flask(__name__)
class WxLoginView(MethodView):
_qrcode = None
def get(self):
# 开启线程,不然不登录会阻塞住
Thread(target=self.login).start()
# 获取二维码,这是这个递归函数,最后返回的一定是二维码
qrcode = self.get_qrcode()
return Response(qrcode, mimetype='image/jpeg')
def login(self):
# 执行登录动作
bot = Bot(qr_callback=self.qr_callback)
def qr_callback(self, uuid, status, qrcode):
# 将拿到的二维码赋值给self._qrcode
self._qrcode = qrcode
def get_qrcode(self):
# 休眠0.1秒,等拿到了二维码之后再去返回,为了避免为空, 写成递归函数
sleep(0.1)
return self._qrcode if self._qrcode is not None else self.get_qrcode()
app.add_url_rule('/login', view_func=WxLoginView.as_view('login'))
if __name__ == '__main__':
app.run()
标签:__,登录,get,wxpy,self,二维码,qrcode,import 来源: https://www.cnblogs.com/ivy-blogs/p/12114691.html