https://www.cnblogs.com/myzhibie/p/4470065.html
https://www.cnblogs.com/Wishise/p/4290073.html
server端:
server.py
# coding=utf8
# !/usr/bin/python
import struct, socket, sys
import hashlib
import threading, random
import time
from base64 import b64encode, b64decode
connectionlist = {}
# python3k 版本recv返回字节数组
def decode(data):
print(data)
if not len(data):
return False
length = data[1] & 127
if length == 126:
mask = data[4:8]
raw = data[8:]
elif length == 127:
mask = data[10:14]
raw = data[14:]
else:
mask = data[2:6]
raw = data[6:]
ret = ''
for cnt, d in enumerate(raw):
ret += chr(d ^ mask[cnt % 4])
return ret
def encode(data):
data = str.encode(data)
head = b'\x81'
if len(data) < 126:
head += struct.pack('B', len(data))
elif len(data) <= 0xFFFF:
head += struct.pack('!BH', 126, len(data))
else:
head += struct.pack('!BQ', 127, len(data))
return head + data
def sendMessage(message):
global connectionlist
for connection in connectionlist.values():
connection.send(encode(message))
def deleteconnection(item):
global connectionlist
del connectionlist['connection' + item]
class WebSocket(threading.Thread):
GUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11"
def __init__(self, conn, index, name, remote, path="/"):
threading.Thread.__init__(self)
self.conn = conn
self.index = index
self.name = name
self.remote = remote
self.path = path
self.buffer = ""
def run(self):
print('Socket%s Start!' % self.index)
headers = {}
self.handshaken = False
while True:
if self.handshaken == False:
print('Socket%s Start Handshaken with %s!' % (self.index, self.remote))
self.buffer += bytes.decode(self.conn.recv(1024))
if self.buffer.find('\r\n\r\n') != -1:
header, data = self.buffer.split('\r\n\r\n', 1)
for line in header.split("\r\n")[1:]:
key, value = line.split(": ", 1)
headers[key] = value
headers["Location"] = ("ws://%s%s" % (headers["Host"], self.path))
key = headers['Sec-WebSocket-Key']
token = b64encode(hashlib.sha1(str.encode(str(key + self.GUID))).digest())
handshake = "HTTP/1.1 101 Switching Protocols\r\n" \
"Upgrade: websocket\r\n" \
"Connection: Upgrade\r\n" \
"Sec-WebSocket-Accept: " + bytes.decode(token) + "\r\n" \
"WebSocket-Origin: " + str(
headers["Origin"]) + "\r\n" \
"WebSocket-Location: " + str(headers["Location"]) + "\r\n\r\n"
self.conn.send(str.encode(str(handshake)))
self.handshaken = True
print('Socket%s Handshaken with %s success!' % (self.index, self.remote))
sendMessage('Welcome, ' + self.name + ' !')
else:
msg = decode(self.conn.recv(1024))
if msg == 'quit':
print('Socket%s Logout!' % (self.index))
nowTime = time.strftime('%H:%M:%S', time.localtime(time.time()))
sendMessage('%s %s say: %s' % (nowTime, self.remote, self.name + ' Logout'))
deleteconnection(str(self.index))
self.conn.close()
break
else:
print('Socket%s Got msg:%s from %s!' % (self.index, msg, self.remote))
nowTime = time.strftime('%H:%M:%S', time.localtime(time.time()))
sendMessage('%s %s say: %s' % (nowTime, self.remote, msg))
self.buffer = ""
class WebSocketServer(object):
def __init__(self):
self.socket = None
def begin(self):
print('WebSocketServer Start!')
self.socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.socket.bind(("127.0.0.1", 12345))
self.socket.listen(5)
global connectionlist
i = 0
while True:
connection, address = self.socket.accept()
username = address[0]
newSocket = WebSocket(connection, i, username, address)
newSocket.start()
connectionlist['connection' + str(i)] = connection
i = i + 1
if __name__ == "__main__":
server = WebSocketServer()
server.begin()
python client 端
https://segmentfault.com/q/1010000009284816
client.py
# install ws4py
# pip install ws4py
# easy_install ws4py
from ws4py.client.threadedclient import WebSocketClient
class DummyClient(WebSocketClient):
def opened(self):
self.send("www.baidu.com")
def closed(self, code, reason=None):
print("Closed down", code, reason)
def received_message(self, m):
print(m)
if __name__ == '__main__':
try:
ws = DummyClient('ws://127.0.0.1:12345', protocols=['chat'])
ws.connect()
ws.run_forever()
except KeyboardInterrupt:
ws.close()
html client端
html>
<head>
<meta charset="utf-8">
head>
<body>
<h3>WebSocketTesth3>
<div id="login">
<div>
<input id="serverIP" type="text" placeholder="服务器IP" value="127.0.0.1" autofocus="autofocus" />
<input id="serverPort" type="text" placeholder="服务器端口" value="9002" />
<input id="btnConnect" type="button" value="连接" onclick="connect()" />
div>
<div>
<input id="sendText" type="text" placeholder="发送文本" value="I'm WebSocket Client!" />
<input id="btnSend" type="button" value="发送" onclick="send()" />
div>
<div>
<div>
来自服务端的消息
div>
<textarea id="txtContent" cols="50" rows="10" readonly="readonly">textarea>
div>
div>
body>
<script>
var socket;
function connect() {
var host = "ws://" + $("serverIP").value + ":" + $("serverPort").value + "/"
socket = new WebSocket(host);
try {
socket.onopen = function (msg) {
$("btnConnect").disabled = true;
alert("连接成功!");
};
socket.onmessage = function (msg) {
if (typeof msg.data == "string") {
displayContent(msg.data);
}
else {
alert("非文本消息");
}
};
socket.onclose = function (msg) { alert("socket closed!") };
}
catch (ex) {
log(ex);
}
}
function send() {
var msg = $("sendText").value
socket.send(msg);
}
window.onbeforeunload = function () {
try {
socket.close();
socket = null;
}
catch (ex) {
}
};
function $(id) { return document.getElementById(id); }
Date.prototype.Format = function (fmt) { //author: meizz
var o = {
"M+": this.getMonth() + 1, //月份
"d+": this.getDate(), //日
"h+": this.getHours(), //小时
"m+": this.getMinutes(), //分
"s+": this.getSeconds(), //秒
"q+": Math.floor((this.getMonth() + 3) / 3), //季度
"S": this.getMilliseconds() //毫秒
};
if (/(y+)/.test(fmt)) fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(fmt)) fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
return fmt;
}
function displayContent(msg) {
$("txtContent").value += "\r\n" +new Date().Format("yyyy/MM/dd hh:mm:ss")+ ": " + msg;
}
function onkey(event) { if (event.keyCode == 13) { send(); } }
script>
html>