• 想实现网页前端和后端的数据同步交互,就有必要使用 websocket 的方式进行通信。

  • python websocket github 地址:git@github.com:Aplexchenfl/python-websocket-server.git

  • 下载之后:

    ECM_5412@chenfl:/var/www/html$ ls
client.html README.md server.py setup.py tests websocket_server
  • 其中,服务端的代码如下:

    vim server.py
    from websocket_server import WebsocketServer
// 当新的客户端连接时会提示
# Called for every client connecting (after handshake)
def new_client(client, server):
print("New client connected and was given id %d" % client['id'])
server.send_message_to_all("Hey all, a new client has joined us") // 当旧的客户端离开
# Called for every client disconnecting
def client_left(client, server):
print("Client(%d) disconnected" % client['id']) // 接收客户端的信息。
# Called when a client sends a message
def message_received(client, server, message):
if len(message) > 200:
message = message[:200]+'..'
print("Client(%d) said: %s" % (client['id'], message)) PORT=9001
server = WebsocketServer(PORT, "0.0.0.0")
server.set_fn_new_client(new_client)
server.set_fn_client_left(client_left)
server.set_fn_message_received(message_received)
server.run_forever()
    // 通过 python3  启动这个客户端
python3 server.py
  • 客户端代码如下:

    <html>
<head>
<title>Simple client</title> <script type="text/javascript"> var ws; function init() {
// 获取服务端ip
var ip_addr = document.location.hostname;
window.WebSocket = window.WebSocket || window.MozWebSocket;
ws = new WebSocket('ws://' + ip_addr +':9001'); // 申请新的客户端 // Connect to Web Socket
//ws = new WebSocket("ws://localhost:9001/"); // Set event handlers.
ws.onopen = function() {
output("onopen");
}; ws.onmessage = function(e) {
// e.data contains received string.
output("onmessage: " + e.data);
}; ws.onclose = function() {
output("onclose");
}; ws.onerror = function(e) {
output("onerror");
console.log(e)
}; } function onSubmit() {
var input = document.getElementById("input");
// You can send message to the Web Socket using ws.send.
ws.send(input.value);
output("send: " + input.value);
input.value = "";
input.focus();
} function onCloseClick() {
ws.close();
} function output(str) {
var log = document.getElementById("log");
var escaped = str.replace(/&/, "&amp;").replace(/</, "&lt;").
replace(/>/, "&gt;").replace(/"/, "&quot;"); // "
log.innerHTML = escaped + "<br>" + log.innerHTML;
} </script>
</head>
<body onload="init();">
<form onsubmit="onSubmit(); return false;">
<input type="text" id="input">
<input type="submit" value="Send">
<button onclick="onCloseClick(); return false;">close</button>
</form>
<div id="log"></div>
</body>
</html>
    //  通过网页启动客户端。

websocket 初步使用经验(python)的更多相关文章

  1. 初步学习python

    自计算机诞生以来,也伴随着计算机语言的诞生,现在,全世界的编程语言有600多种,但流行的编程语言也就20多种. Java和C一直占据着前两名.但是近年来伴随着人工智能的发展,Python发展迅猛,以其 ...

  2. Day1 初步认识Python

    天气有点阴晴不定~ (截图来自----------金角大王) 1.学习了计算机概论(CPU/Memory/Disk,memory的存在是为了解决信息传输产生的时延) CPU:精简指令集(RISC)-- ...

  3. WebSocket client for python

    Project description websocket-client module is WebSocket client for python. This provide the low lev ...

  4. 孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块

    孤荷凌寒自学python第六十七天初步了解Python爬虫初识requests模块 (完整学习过程屏幕记录视频地址在文末) 从今天起开始正式学习Python的爬虫. 今天已经初步了解了两个主要的模块: ...

  5. OKEX websocket API 连接Python范例

    因为 websocket-client 新版的各种大脑降级设计 很多功能无法使用需要安装老版本websocket-client的包才能正常使用 pip3 install websocket-clien ...

  6. (一)初步了解python

    python的创始人为吉多·范罗苏姆(Guido van Rossum).1989年的圣诞节期间,吉多·范罗苏姆为了在阿姆斯特丹打发时间,决心开发一个新的脚本解释程序,作为ABC语言的一种继承. Py ...

  7. 初步理解Python进程的信号通讯

    Reference: http://www.jb51.net/article/63787.htm 信号的概念 信号(signal)--     进程之间通讯的方式,是一种软件中断.一个进程一旦接收到信 ...

  8. python初步学习-Python模块之 re

    re 正则表达式 python正则表达式在线检验网站 python re正则表达式语法 匹配字符 语法 解释 表达式 匹配实例 . 匹配任意除"\n"以外的任何字符 a.c abc ...

  9. python初步学习-python 模块之 json

    json 模块 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,易于人阅读和编写.一般API返回的数据大多是 JSON.XML,如果返回JSON的话,将获取 ...

随机推荐

  1. JavaScript-event参数传递详解

    onmouseover="over(event)" onmouseout="out(event)" onclick="change(event)&qu ...

  2. DataSnap使用UniDac处理自增长字段

    原来使用ado来访问数据库,用在DataSnap中也很方便.后来便一直使用UniDac,可发现UniDac如果用在DataSnap中要比ado麻烦很多,尤其对自增长字段.缺省值的处理上,感觉对Data ...

  3. web development blog(转)

    Top 10 jQuery Mobile Code Snippets that you need to know jQuery Mobile is a framework for mobile web ...

  4. 一种时间复杂度为O(n)的排序方法(转载)

    原文地址:http://my.oschina.net/u/158457/blog/28536 排序的方法很特别,有点类似插入排序的味道 先看下代码 public class Sort { privat ...

  5. Redis 实现队列优先级

    通常使用一个list来实现队列操作,这样有一个小限制,所以的任务统一都是先进先出,如果想优先处理某个任务就不太好处理了,这就需要让队列有优先级的概念,我们就可以优先处理高级别的任务. 实现方式: (1 ...

  6. PHPEXCEL导出excel表格中长数字文本自动转为科学计数法的解决办法

    方法一:前面加空格 $objActSheet->setCellValue('A1', ' '.'330602198804224688'); 方法二: $objActSheet->setCe ...

  7. Linux安装Qt详细步骤 亲测总结

    下载 qt-everywhere-opensource-src-4.8.4.tar.gz================准备工作====================yum install kern ...

  8. jQuery attr方法修改onclick值

    了通过jQuery的attr修改onclick值. 代码: var js = "alert('B:' + this.id); return false;"; var newclic ...

  9. asp.net MVC学习的一些总结

    起初认为视图,控制器,模型它们是完全没有耦合的,真正用了一段时间MVC发现错了. 但通过抽象让他们完全没有耦合,也不是不可能. 1.奇怪的连接地址 用MVC之前,一直认为页面必然访问某个文件.用了MV ...

  10. 关于XILINX芯片IO管脚的上拉电阻的疑问

    XILINX的每个IO脚都有一个可选的可配上拉电阻功能,现在我在配置文件的UCF里使用了这个上拉电阻:语法如下:NET"I_key_data"        LOC = " ...