Joe 的 Erlang websocket 示例的 Python 示例
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2153294/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python example of Joe's Erlang websocket example
提问by MattyW
I've just been working through the erlang websockets example from Joe Armstrong's blogI'm still quite new to erlang so I decided to write a simple server in python that would help teach me about websockets (and hopefully some erlang by interpreting joe's code). I'm having two issues:
我刚刚研究了Joe Armstrong 博客中的 erlang websockets 示例,我对 erlang 还是很陌生,所以我决定用 python 编写一个简单的服务器,这将有助于教我有关 websockets 的知识(并希望通过解释 joe 的代码来了解一些 erlang) . 我有两个问题:
1) Data I receive from the page includes a '?' as the last char. This doesn't appear in the erlang version and I can't work out where it's coming from Fixed - This was because the strings where encoded in utf-8 and I wasn't decoding them
1) 我从页面收到的数据包括一个“?” 作为最后一个字符。这没有出现在 erlang 版本中,我无法弄清楚它来自Fixed 的地方 - 这是因为字符串以 utf-8 编码而我没有解码它们
2) I seem to be sending data from the server (through the websocket) - which can be confirmed by looking at how many bytes client.send() makes. But nothing is appearing on the page. Fixed, I wasn't encoding the string correctly
2)我似乎正在从服务器发送数据(通过 websocket) - 这可以通过查看 client.send() 产生多少字节来确认。但页面上什么也没有出现。已修复,我没有正确编码字符串
I've put all the code here. Here's my python version incase i'm missing anything obvious
我把所有的代码都放在这里了。这是我的 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()
For those who haven't run through joe's example but still want to help, you just need to serve up interact.html through a web server and then start your server (The code assumes the webserver is running on localhost:8888)
对于那些还没有完成 joe 示例但仍想提供帮助的人,您只需要通过 Web 服务器提供 interact.html,然后启动您的服务器(代码假设 Web 服务器在 localhost:8888 上运行)
回答by MattyW
For those who are interested this was the solution
对于那些感兴趣的人,这是解决方案
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()
Edit for liwp's request:
编辑 liwp 的请求:
You can view a diff of the file here. Essentially my problem was the way I was decoding / encoding strings prior to sending / receiving. There's a websocket modulebeing worked on for Apache on google code which I used to find out where I was going wrong.
您可以在此处查看文件的差异。基本上我的问题是我在发送/接收之前解码/编码字符串的方式。有一个websocket 模块正在谷歌代码上为 Apache 工作,我用来找出我出错的地方。
回答by bernardw
Thanks for sharing the code. I ran into one problem running this code in Windows. Which I think it might help for people who are still figuring.
感谢分享代码。我在 Windows 中运行此代码时遇到了一个问题。我认为这可能对仍在思考的人有所帮助。
I strink the space, so that it bacame 'Upgrade: WebSocket'
Make sure your hosting page is matching the Origin, which in this case it's 'http://localhost:8888'
我缩小了空间,使其成为“升级:WebSocket”
确保您的托管页面与 Origin 匹配,在本例中为“ http://localhost:8888”
It's working beautifully for me now.
它现在对我来说效果很好。
回答by Ben Ford
Eventlet has websocket support built in and stargate is a package for using websockets with the pyramid web framework: http://boothead.github.com/stargate/
Eventlet 内置了 websocket 支持,而 stargate 是一个将 websockets 与金字塔 web 框架一起使用的包:http: //boothead.github.com/stargate/