将 python 客户端服务器转换为 websocket
我创建了一个简单的回显服务器,我想将它用于一个简单的网络服务器示例。
到目前为止,我拥有服务器:
Server.py
from socket import *
import threading
import thread
def handler(clientsock,addr):
while 1:
data = clientsock.recv(BUFSIZ)
if not data:
break
msg = 'echoed:... ' + data
clientsock.send(msg)
clientsock.close()
if __name__=='__main__':
HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)
while 1:
print 'waiting for connection...'
clientsock, addr = serversock.accept()
print '...connected from:', addr
thread.start_new_thread(handler, (clientsock, addr))
我想使用websockify
,我发现它能够使用以下方式包装现有程序:
./websockify 2023 -- PROGRAM ARGS
如何使用上述命令运行以前的服务器? 我可以在一台计算机上运行 websockify
而 server.py 在另一台计算机上运行吗?如果是。我如何修改上述命令?
有人可以提供一个简单的 HTML5 示例来向我展示 client.html
应该是什么样子吗?请记住,我希望能够在不支持本机 websocket 的浏览器中使用该示例。我认为 websockify 可以使用 web-socket-js 来做到这一点。
他们指出:它对二进制数据具有透明的支持,并且如果浏览器本身不支持 WebSockets,则可以自动回退到 web-socket-js Flash Websocket shim/polyfill
I create a simple echo server and I would like to use it for a simple webserver example.
Until now I have the server:
Server.py
from socket import *
import threading
import thread
def handler(clientsock,addr):
while 1:
data = clientsock.recv(BUFSIZ)
if not data:
break
msg = 'echoed:... ' + data
clientsock.send(msg)
clientsock.close()
if __name__=='__main__':
HOST = 'localhost'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
serversock = socket(AF_INET, SOCK_STREAM)
serversock.bind(ADDR)
serversock.listen(2)
while 1:
print 'waiting for connection...'
clientsock, addr = serversock.accept()
print '...connected from:', addr
thread.start_new_thread(handler, (clientsock, addr))
I would like to use websockify
and I found that is able to wrap an existing program using:
./websockify 2023 -- PROGRAM ARGS
How can I run the the previous server using the above command? Can I run websockify
on one machine while the server.py is running on a different machine? If yes. How can I modify the above command?
Can someone provide a simple HTML5 example to show me how the client.html
should look like? Please keep in mind that I want to be able to use the example with browsers that do not have native websocket support. I think websockify is able to do that using web-socket-js
.
They state: It has transparent support for binary data and has automatic fallback to the web-socket-js Flash Websocket shim/polyfill if the browser does not support WebSockets natively
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
要与 websockify 交互,我建议您客户端 HTML/Javascript 使用 include/websock.js 提供的库。 websock.js API 与标准 WebSocket 略有不同。
我刚刚将一个非常简单的 HTML 客户端页面推送到 websockify 存储库中。
要将 server.py 包装在同一系统上,您可以像这样运行 websockify:
在“http://localhost:21567/tests/simple.html”加载简单客户端。输入“ws://localhost:21567”作为 websocket URI 并点击连接。请注意,环绕模式使用重新绑定,因此您必须构建将侦听端口移开的方式,以便 websockify 可以为您进行干预。
您还可以在同一系统上单独运行 websockify:
在这种情况下,您将浏览到“http://localhost:6080/tests/simple.html”并使用“ws://localhost:6080”作为连接的 URI到。
如果您想在与运行 server.py 的计算机不同的计算机上运行 websockify,则需要更改 server.py 以侦听公共接口而不仅仅是“localhost”。进行更改后,如果 server.py 在 192.168.1.7 上运行,那么您可以在 192.168.1.5 上运行 websockify,如下所示:
在 192.168.1.7 上启动 server.py:
在 192.168.1.5 上启动 websockify 并将其指向 server.py在另一个系统上:
然后您将浏览到“http://192.168.1.5:6080/tests/simple.html”并使用 URI“ws://192.168.1.5:6080”。
或者,如果您只是希望您的服务器成为 WebSocket 服务器,那么您可以只使用 websocket.py 库,而不需要完整的 websockify 功能,并直接创建您的服务器。请参阅 websockify 附带的 tests/echo.py 示例一个简单的 WebSocket 回显服务器。
To interface with websockify I recommend that you client HTML/Javascript use the library provided by include/websock.js. The websock.js API is a bit different than standard WebSockets.
I just pushed a very simple HTML client page into the websockify repository.
To wrap your server.py on the same system, you would run websockify like this:
The load the simple client at "http://localhost:21567/tests/simple.html". Enter "ws://localhost:21567" as the websocket URI and hit connect. Note that the wrap mode uses a rebind.so that you have to build which moves the listen port out of the way so that websockify can interpose for you.
You can also run websockify separately on the same system:
In that case you'll browse to "http://localhost:6080/tests/simple.html" and use "ws://localhost:6080" as the URI to connect to.
If you want to run websockify on a different machine from the one where server.py is running then you will need to change server.py to listen on a public interface and not just "localhost". Once you make that change, if server.py is running on 192.168.1.7 then you could run websockify on 192.168.1.5 like this:
On 192.168.1.7 launch server.py:
On 192.168.1.5 launch websockify and point it at server.py on the other system:
Then you would browse to "http://192.168.1.5:6080/tests/simple.html" and use URI "ws://192.168.1.5:6080".
Alternately, if you are just wanting your server to be a WebSocket server then you can just use the websocket.py library without the full websockify functionality and make your server directly. See the tests/echo.py example included with websockify which is a simple WebSocket echo server.