Python中使用dwebsocket實(shí)現(xiàn)后端數(shù)據(jù)實(shí)時(shí)刷新
執(zhí)行定時(shí)任務(wù)的時(shí)候,我們需要了解執(zhí)行百分比或者實(shí)時(shí)數(shù)據(jù)返回,這時(shí)候可以采用的方法
1.ajax請(qǐng)求后端服務(wù)器,然后前端頁面局部渲染獲取百分比
2.使用webscoket進(jìn)行長連接交流刷新
ajax使用方法使用interval函數(shù)來實(shí)現(xiàn)定時(shí)請(qǐng)求,本次這里不做說明
views.py文件添加如下內(nèi)容
from django.shortcuts import render,HttpResponse from dwebsocket.decorators import accept_websocket import time,random import uuid import json @accept_websocket def test_websocket(request): cnt=1 if request.is_websocket(): while True: messages = { 'time': time.strftime('%Y.%m.%d %H:%M:%S', time.localtime(time.time())), 'server_msg': 'hello%s'%time.time(), 'client_msg': 'msg%s'%time.time() } time.sleep(1) cnt+=1 if cnt<=10: request.websocket.send(json.dumps(messages)) else: break def test_websocket_client(request): return render(request,'websocket_client.html',locals())
settings.py文件增加dwebsocket
INSTALLED_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'dwebsocket'
]
urls.py文件添加相關(guān)鏈接
urlpatterns = [ path('test_websocket', views.test_websocket, name='test_websocket'), path('test_websocket_client', views.test_websocket_client, name='test_websocket_client'), ]
直接上html代碼
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>dwebsocket實(shí)踐</title> <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> <script type="text/javascript"> $(function () { // $('#send_message').click( // function() { var socket = new WebSocket("ws://" + window.location.host + "/test_websocket"); socket.onopen = function () { console.log('WebSocket open');//成功連接上Websocket // socket.send($('#message').val());//發(fā)送數(shù)據(jù)到服務(wù)端 }; socket.onmessage = function (e) { // console.log('message: ' + e.data);//打印服務(wù)端返回的數(shù)據(jù) $('#messagecontainer').text('<p>' + JSON.parse(e.data).client_msg + '</p>'+'<p>' + JSON.parse(e.data).server_msg + '</p>'); // $('#messagecontainer').text('<p>' + JSON.parse(e.data).server_msg + '</p>'); }; socket.onclose=function () { console.log("連接已關(guān)閉") } // }); }); </script> </head> <body> <input type="text" id="message" value="請(qǐng)輸入發(fā)送消息!" /> <button type="button" id="send_message">send message</button> <h1>接受到消息</h1> <div id="messagecontainer"> </div> </body> </html>
然后我們運(yùn)行程序
十秒之后斷開連接得到了我們想要的結(jié)果
業(yè)務(wù)需求的話,可以在我們的test_websocket 修改我們的邏輯然后根據(jù)返回的結(jié)果進(jìn)行渲染
到此這篇關(guān)于Python中使用dwebsocket實(shí)現(xiàn)后端數(shù)據(jù)實(shí)時(shí)刷新的文章就介紹到這了,更多相關(guān)Python dwebsocket內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- python中使用websocket方法實(shí)例詳解
- 使用python構(gòu)建WebSocket客戶端的教程詳解
- python使用websocket庫發(fā)送WSS請(qǐng)求
- 使用Python創(chuàng)建websocket服務(wù)端并給出不同客戶端的請(qǐng)求
- python開發(fā)實(shí)例之python使用Websocket庫開發(fā)簡單聊天工具實(shí)例詳解(python+Websocket+JS)
- Python Websocket服務(wù)端通信的使用示例
- Python如何使用WebSocket實(shí)現(xiàn)實(shí)時(shí)Web應(yīng)用
相關(guān)文章
使用Python編程分析火爆全網(wǎng)的魷魚游戲豆瓣影評(píng)
本文來為大家介紹如何使用Python爬取影評(píng)的操作,主要是爬取《魷魚游戲》在豆瓣上的一些影評(píng),對(duì)數(shù)據(jù)做一些簡單的分析,用數(shù)據(jù)的角度重新審視下這部劇,有需要的朋友可以借鑒參考下2021-10-10PyQt5重寫QComboBox的鼠標(biāo)點(diǎn)擊事件方法
今天小編就為大家分享一篇PyQt5重寫QComboBox的鼠標(biāo)點(diǎn)擊事件方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-06-06pytest用例間參數(shù)傳遞的兩種實(shí)現(xiàn)方式示例
pytest提供了許多運(yùn)行命令以供定制化運(yùn)行某一類測(cè)試用例或者某個(gè)測(cè)試用例等,下面這篇文章主要給大家介紹了關(guān)于pytest用例間參數(shù)傳遞的兩種實(shí)現(xiàn)方式,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下2021-12-12python對(duì)日志進(jìn)行處理的實(shí)例代碼
本篇文章給大家分享了關(guān)于python處理日志的方法以及相關(guān)實(shí)例代碼,有興趣的朋友們學(xué)習(xí)下。2018-10-10使用python實(shí)現(xiàn)excel的Vlookup功能
這篇文章主要介紹了使用python實(shí)現(xiàn)excel的Vlookup功能,當(dāng)我們想要查找的數(shù)據(jù)量較大時(shí),這時(shí)則有請(qǐng)我們的主角VLookup函數(shù)出場(chǎng),那么如何用python實(shí)現(xiàn)VLookup呢,需要的朋友可以參考下2023-04-04