我刚刚完成了来自Joe Armstrong博客的erlang websockets示例我仍然是erlang的新手所以我决定在python中编写一个简单的服务器,这将有助于教我webockets(并希望通过解释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()
Run Code Online (Sandbox Code Playgroud)
对于那些没有通过joe示例但仍希望提供帮助的人,您只需要通过Web服务器提供interact.html然后启动您的服务器(代码假定Web服务器在localhost:8888上运行)
Mat*_*tyW 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()
Run Code Online (Sandbox Code Playgroud)
编辑liwp的请求:
您可以在此处查看文件的差异.基本上我的问题是我在发送/接收之前解码/编码字符串的方式.有一个websocket模块正在为谷歌代码上的Apache工作,我曾经找到我出错的地方.
| 归档时间: |
|
| 查看次数: |
11628 次 |
| 最近记录: |