9

我刚刚研究了Joe Armstrong 博客中的 erlang websockets 示例。我对 erlang 还是很陌生,所以我决定用 python 编写一个简单的服务器来帮助我了解 websockets(并希望通过解释 joe 的代码来了解一些 erlang) . 我有两个问题:

1)我从页面收到的数据包括一个“ÿ”作为最后一个字符。这没有出现在 erlang 版本中,我无法确定它来自固定 - 这是因为字符串以 utf-8 编码,我没有解码它们

2) 我似乎正在从服务器发送数据(通过 websocket)——这可以通过查看 client.send() 产生的字节数来确认。但是页面上什么也没有出现。已修复,我没有正确编码字符串

我把所有的代码都放在这里了。这是我的 python 版本,以防我遗漏任何明显的东西

import threading
import socket

def start_server():
    tick = 0
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 1234))
    sock.listen(100)
    while True:
        print 'listening...'
        csock, address = sock.accept()
        tick+=1
        print 'connection!' 
        handshake(csock, tick)
        print 'handshaken'
        while True:
            interact(csock, tick)
            tick+=1

def handshake(client, tick):
    our_handshake = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"+"Upgrade:     WebSocket\r\n"+"Connection: Upgrade\r\n"+"WebSocket-Origin:     http://localhost:8888\r\n"+"WebSocket-Location: "+" ws://localhost:1234/websession\r\n\r\n"
    shake = client.recv(255)
    print shake
    client.send(our_handshake)

def interact(client, tick):
    data = client.recv(255)
    print 'got:%s' %(data)
    client.send("clock ! tick%d\r" % (tick))
    client.send("out ! recv\r")

if __name__ == '__main__':
    start_server()

对于那些没有通过 joe 的示例但仍想提供帮助的人,您只需通过 Web 服务器提供 interact.html 然后启动您的服务器(代码假设 Web 服务器在 localhost:8888 上运行)

4

3 回答 3

10

对于那些感兴趣的人,这是解决方案

import threading
import socket

def start_server():
    tick = 0
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind(('localhost', 1234))
    sock.listen(100)
    while True:
        print 'listening...'
        csock, address = sock.accept()
        tick+=1
        print 'connection!' 
        handshake(csock, tick)
        print 'handshaken'
        while True:
            interact(csock, tick)
            tick+=1
            
            
def send_data(client, str_):
    #_write(request, '\x00' + message.encode('utf-8') + '\xff')
    str_ = '\x00' + str_.encode('utf-8') + '\xff'
    return client.send(str_)
def recv_data(client, count):
    data = client.recv(count)    
    return data.decode('utf-8', 'ignore')

def handshake(client, tick):
    our_handshake = "HTTP/1.1 101 Web Socket Protocol Handshake\r\n"+"Upgrade:     WebSocket\r\n"+"Connection: Upgrade\r\n"+"WebSocket-Origin: http://localhost:8888\r\n"+"WebSocket-Location: "+" ws://localhost:1234/websession\r\n\r\n"
    shake = recv_data(client, 255)
    print shake
    #We want to send this without any encoding
    client.send(our_handshake)
         
def interact(client, tick):
    data = recv_data(client, 255)
    print 'got:%s' %(data)
    send_data(client, "clock ! tick%d" % (tick))
    send_data(client, "out ! %s" %(data))

if __name__ == '__main__':
    start_server()

编辑 liwp 的请求:

您可以在此处查看文件的差异。本质上我的问题是我在发送/接收之前解码/编码字符串的方式。在 google 代码上正在为 Apache 开发一个websocket 模块,我曾经用它来找出我哪里出错了。

于 2010-01-28T10:48:54.697 回答
0

感谢分享代码。我在 Windows 中运行此代码时遇到了一个问题。我认为这可能对仍在思考的人有所帮助。

  1. 我整理了空间,所以它变成了“升级:WebSocket”

  2. 确保您的托管页面与 Origin 匹配,在本例中为“ http://localhost:8888

它现在对我来说工作得很好。

于 2010-08-08T08:46:30.373 回答
0

Eventlet 内置了 websocket 支持,stargate 是一个用于将 websockets 与金字塔 web 框架一起使用的包:http: //boothead.github.com/stargate/

于 2011-01-18T16:38:12.443 回答