Python实现WEB详细过程_第1页
Python实现WEB详细过程_第2页
Python实现WEB详细过程_第3页
Python实现WEB详细过程_第4页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

使用Websocket对于客户端来说无疑十分简单。websocket提供了三个简单的函数,onopen,onclose以及onmessage,顾名思义,他们分别监听socket的开启、断开和消息状态。例如在一个WebSocket的客户端例子中,你可以这样写:Web Socket Examplewindow.onload = function() var s = new WebSocket(“ws:/localhost:8000/”);s.onopen = function(e) alert(“opened”); s.onclose = function(e) alert(“closed”); s.onmessage = function(e) alert(“got: ” + e.data); ;在代码中,首先创建了一个新的socket,指向websocket服务器端:var s = new WebSocket(“ws:/localhost:8000/”);然后利用三个基本函数实现状态监听。当然这个例子并不完善,它参考了麻省理工一位研究生Mr. Yang的文章(/wp/web-sockets-tutorial-with-simple-python-server/),而你如果需要更好的客户端测试代码,可以看这里:http:/cl.ly/3N3Y3t2s3U1v1h0A433u我们在新的代码里提供了完成的和服务器交互数据的功能,发送数据我们使用的函数是send(),你可以在代码中看到。我们之所以不直接引用Yang的文章是因为Yang对服务器的理解不够完善,或者用他的话来说就是outdate。感谢Yang为我们提供了一个简单的socket server的例子,不过可惜放在现在来说它是有问题的,当然我们还是把例子引述如下:#!/usr/bin/env pythonimport socket, threading, timedef handle(s):print repr(s.recv(4096)s.send(”HTTP/1.1 101 Web Socket Protocol HandshakerUpgrade: WebSocketrConnection: UpgraderWebSocket-Origin: http:/localhost:8888rWebSocket-Location: ws:/localhost:9876/rWebSocket-Protocol: sample”.strip() + rnrn)time.sleep(1)s.send(x00helloxff)time.sleep(1)s.send(x00worldxff)s.close()s = socket.socket()s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)s.bind(”, 9876);s.listen(1);while 1:t,_ = s.accept();threading.Thread(target = handle, args = (t,).start()虽然不符合我的风格,不过作为python代码来说依然是极具优美。我们可以看到,Yang在代码中首先创建了一个socket,然后进行握手,此处的例子是:s.send(”HTTP/1.1 101 Web Socket Protocol HandshakerUpgrade: WebSocketrConnection: UpgraderWebSocket-Origin: http:/localhost:8888rWebSocket-Location: ws:/localhost:9876/rWebSocket-Protocol: sample”.strip() + rnrn)向客户端发送了这样一串字符串作为data的head信息以完成握手。当然如果你认真的去测试了Yang的例子,就会发现这样做是不规范的,或者说是不符合当前的Websocket协议的。控制台会反馈给你这样的信息:miss head: Sec-WebSocket-Localmiss head: sec-WebSocket-Origin这是因为,在第76和77版的websocket协议中,握手远非Yang所做的这般简单。在draft protocol 76中,具体的握手要求如下1.Client应从WebSocket-Origin发送head包到WebSocket-Location2.客户端发送的包结构应该是包括Host,Origin,Sec-WebSocket-Key1, Sec-WebSocket-Key2等信息:这是我们截取的一个例子:GET / HTTP/1.1rnUpgrade: WebSocketrnConnection: UpgradernHost: localhost:9876rnOrigin:rnSec-WebSocket-Key1: c 33w T5 1 1C72 66 I E=r 8rnSec-WebSocket-Key2: 354214h9998 f rn3.服务器断获取包后,应该提取总长度为20的sec-websocket-key,然后进行如下计算:For each of these fields, the server has to take the digits from the value to obtain a number (in this case 1868545188 and 1733470270 respectively), then divide that number by the number of spaces characters in the value (in this case 12 and 10) to obtain a 32-bit number (155712099 and 173347027). These two resulting numbers are then used in the server handshake, as described below.既首先把以上字符串转化成数字,然后再计算key中的空格数目,并用key值除以空格数得到最后key值。4.发送包含以下信息的head给client,完成握手,这里我直接贴出代码。csock.send(“HTTP/1.1 101 WebSocket Protocol Handshakern”)csock.send(“Upgrade: WebSocketrn”)csock.send(“Connection: Upgradern”)csock.send(“Sec-WebSocket-Origin: http:/”+origin+”rn”)csock.send(“Sec-WebSocket-Location: ws:/”+host+”:”+str(port)+”/rn”)csock.send(“Sec-WebSocket-Protocol: chatrn”)csock.send(“rn”)5.在完成握手即可进行数据交互,特别注意的是数据发送应当以0X00开头以0Xff结尾:因而

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论