python-圖片流傳輸?shù)乃悸芳笆纠?url轉(zhuǎn)換二維碼)
1.場景
- 將URL動態(tài)生成二維碼前端展示(微信支付等,)--》
1.靜態(tài)文件路徑訪問
返回URL_name,(a標(biāo)簽,src 靜態(tài)路由訪問)
2.流傳輸,前端渲染
二進(jìn)制流返回前端,前端根據(jù)二進(jìn)制流編碼類型顯示
3.前端js生成
后臺獲取到微信支付的code_url,前端js將code_url生成二維碼,并渲染
- 實(shí)際代碼
使用python_web 框架--》tornado
manager.py
import os
import asyncio
import tornado.ioloop
import tornado.httpserver
import tornado.web
import tornado.options
from tornado.options import define, options, parse_command_line
from apps import UrlHandler, Url2Handler, Url3Handler
define("port", default=8000, type=int)
def create_app():
settings = {
"template_path": os.path.join(os.path.dirname(__file__), "templates"),
"static_path": os.path.join(os.path.dirname(__file__), "static"),
}
application = tornado.web.Application(
handlers=[
(r"/url", UrlHandler),
(r"/url2", Url2Handler),
(r"/url3", Url3Handler),
],
debug=True,
**settings,
)
return application
if __name__ == '__main__':
parse_command_line()
app = create_app()
server = tornado.httpserver.HTTPServer(app)
server.listen(options.port)
asyncio.get_event_loop().run_forever()
apps.py
import tornado.web
from manager_handler import gen_qrcode, gen_qrcode_obj,gen_qrcode_buf
class BaseHandler(tornado.web.RequestHandler):
pass
class UrlHandler(BaseHandler):
def get(self):
# 獲取鏈接
self.render('qrcode.html', title='url', data='URL-提交', img_stream='')
async def post(self):
# 生成二維碼
url = self.get_argument('url_str')
# URL轉(zhuǎn)換二維碼
img_stream = gen_qrcode(url)
await self.render('qrcode.html', title='qrcode', data='掃碼支付', img_stream=img_stream)
class Url2Handler(BaseHandler):
def get(self):
# 獲取鏈接
self.render('qrcode.html', title='url', data='URL-提交', img_stream='')
async def post(self):
# 生成二維碼
url = self.get_argument('url_str')
# URL轉(zhuǎn)換二維碼
img_stream = gen_qrcode_obj(url=url)
# await self.render('qrcode.html', title='qrcode', data='掃碼支付', img_stream=img_stream)
self.set_header('Content_Type', 'image/jpg')
self.set_header('Content_length', len(img_stream))
self.write(img_stream)
class Url3Handler(BaseHandelr):
def get(self):
self.render('qrcode.html', title='url', data='URL-提交', img_stream='')
def post(self):
url = self.get_argument('url')
img_stream = gen_qrcode_buf(url)
self.set_header('Content-Type', 'image/png')
self.write(img_stream)
manager_handler.py
import qrcode
import io
import base64
import time
def gen_qrcode(url):
"""
方式1: URL轉(zhuǎn)換二維碼
:param url: 轉(zhuǎn)換二維碼的URL
:return: base64編碼后的 二進(jìn)制流 二維碼數(shù)據(jù)
"""
qr = qrcode.make(url)
buf = io.BytesIO()
qr.save(buf)
img_buf = buf.getvalue()
img_stream = base64.b64encode(img_buf)
return img_stream
def gen_qrcode_obj(version=1, box_size=10, border=4, url=None):
"""
方式2: URL轉(zhuǎn)換二維碼(圖片流傳輸, template需要指明 data:base64編碼)
:param version:
:param box_size:
:param border:
:return:
"""
qr = qrcode.QRCode(
version=version,
error_correction=qrcode.constants.ERROR_CORRECT_L,
box_size=box_size,
border=border,
)
url = "https://www.12dms.com" if url is None else url
save_name = "./" + "qrcode" + str(time.time()) + ".png"
qr.add_data(url)
qr.make()
img = qr.make_image()
img.save(save_name.encode())
with open(save_name, 'rb') as img_f:
img_stream = img_f.read()
img_stream = base64.b64encode(img_stream)
print(img_stream)
return img_stream
def gen_qrcode_buf(words):
qr = qrcode.make(words)
buf = io.BytesIO()
qr.save(buf, 'png')
qr_buf = buf.getvalue()
# img_stream = base64.b64encode(qr_buf)
return qr_buf
base.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>{% block title %}{% end %}</title>
{% block head %}{% end %}
</head>
<body>
<h1 style="text-align: center">
{% block h1 %}{{ data }}{% end %}
</h1>
{% block content %}{% end %}
</body>
</html>
qrcode.html
{% extends "base.html" %}
{% block title %}
{{ title }}
{% end %}
{% block h1 %}
{{ data }}
{% end %}
{% block content %}
<form method="post" action="" >
<p>
輸入待轉(zhuǎn)換的URL:<input name="url_str"/>
<br>
{# {{ img_stream }}#}
{% if img_stream %}
<img style="width:180px" src="data:;base64,{{ img_stream }}" alt="">
{% end %}
</p>
<br>
<input id="submit" type="submit" value="生成二維碼">
</form>
{% end %}
以上就是python-圖片流傳輸?shù)乃悸芳笆纠?url轉(zhuǎn)換二維碼)的詳細(xì)內(nèi)容,更多關(guān)于python 圖片流傳輸?shù)馁Y料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python在centos7.6上安裝python3.9的詳細(xì)教程(默認(rèn)python版本為2.7.5)
這篇文章主要介紹了Python在centos7.6上安裝python3.9(默認(rèn)python版本為2.7.5)的方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2020-10-10
Sublime如何配置Python3運(yùn)行環(huán)境
這篇文章主要介紹了Sublime如何配置Python3運(yùn)行環(huán)境問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
Python實(shí)現(xiàn)的視頻播放器功能完整示例
這篇文章主要介紹了Python實(shí)現(xiàn)的視頻播放器功能,結(jié)合完整實(shí)例形式分析了Python基于pyglet庫實(shí)現(xiàn)視頻播放功能的相關(guān)操作技巧,需要的朋友可以參考下2018-02-02
python實(shí)現(xiàn)鍵盤控制鼠標(biāo)移動
這篇文章主要為大家詳細(xì)介紹了python實(shí)現(xiàn)鍵盤控制鼠標(biāo)移動,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下2018-10-10
Python編程實(shí)現(xiàn)的簡單神經(jīng)網(wǎng)絡(luò)算法示例
這篇文章主要介紹了Python編程實(shí)現(xiàn)的簡單神經(jīng)網(wǎng)絡(luò)算法,結(jié)合實(shí)例形式分析了神經(jīng)網(wǎng)絡(luò)算法的原理及Python相關(guān)算法實(shí)現(xiàn)技巧,需要的朋友可以參考下2018-01-01
Python機(jī)器學(xué)習(xí)特征重要性分析的8個常用方法實(shí)例探究
本文詳細(xì)介紹8種常用的方法,涵蓋了基于決策樹、集成學(xué)習(xí)模型以及統(tǒng)計(jì)學(xué)方法的特征重要性分析,從決策樹模型到SHAP值,深入探討每種方法的原理和示例,幫助全面了解如何評估特征的重要性,將能更好地理解特征對模型預(yù)測的貢獻(xiàn),為提升模型性能和解釋模型決策提供有力支持2024-01-01
python中sqllite插入numpy數(shù)組到數(shù)據(jù)庫的實(shí)現(xiàn)方法
本文給大家介紹python中sqllite插入numpy數(shù)組到數(shù)據(jù)庫的實(shí)現(xiàn)方法,在文章底部給大家提到了Python 操作sqlite數(shù)據(jù)庫及保存查詢numpy類型數(shù)據(jù)的實(shí)例代碼,需要的朋友參考下吧2021-06-06

