标签归档:websocket

是否有针对Python实现的WebSocket客户端?[关闭]

问题:是否有针对Python实现的WebSocket客户端?[关闭]

我找到了这个项目:http : //code.google.com/p/standalonewebsocketserver/用于WebSocket服务器,但是我需要在python中实现WebSocket客户端,更确切地说,我需要从WebSocket服务器中的XMPP接收一些命令。

I found this project: http://code.google.com/p/standalonewebsocketserver/ for a WebSocket server, but I need to implement a WebSocket client in python, more exactly I need to receive some commands from XMPP in my WebSocket server.


回答 0

http://pypi.python.org/pypi/websocket-client/

简直好用。

 sudo pip install websocket-client

客户端代码示例:

#!/usr/bin/python

from websocket import create_connection
ws = create_connection("ws://localhost:8080/websocket")
print "Sending 'Hello, World'..."
ws.send("Hello, World")
print "Sent"
print "Receiving..."
result =  ws.recv()
print "Received '%s'" % result
ws.close()

样本服务器代码:

#!/usr/bin/python
import websocket
import thread
import time

def on_message(ws, message):
    print message

def on_error(ws, error):
    print error

def on_close(ws):
    print "### closed ###"

def on_open(ws):
    def run(*args):
        for i in range(30000):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
        print "thread terminating..."
    thread.start_new_thread(run, ())


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://echo.websocket.org/",
                                on_message = on_message,
                                on_error = on_error,
                                on_close = on_close)
    ws.on_open = on_open

    ws.run_forever()

http://pypi.python.org/pypi/websocket-client/

Ridiculously easy to use.

 sudo pip install websocket-client

Sample client code:

#!/usr/bin/python

from websocket import create_connection
ws = create_connection("ws://localhost:8080/websocket")
print "Sending 'Hello, World'..."
ws.send("Hello, World")
print "Sent"
print "Receiving..."
result =  ws.recv()
print "Received '%s'" % result
ws.close()

Sample server code:

#!/usr/bin/python
import websocket
import thread
import time

def on_message(ws, message):
    print message

def on_error(ws, error):
    print error

def on_close(ws):
    print "### closed ###"

def on_open(ws):
    def run(*args):
        for i in range(30000):
            time.sleep(1)
            ws.send("Hello %d" % i)
        time.sleep(1)
        ws.close()
        print "thread terminating..."
    thread.start_new_thread(run, ())


if __name__ == "__main__":
    websocket.enableTrace(True)
    ws = websocket.WebSocketApp("ws://echo.websocket.org/",
                                on_message = on_message,
                                on_error = on_error,
                                on_close = on_close)
    ws.on_open = on_open

    ws.run_forever()

回答 1

高速公路为Python提供了一个很好的websocket客户端实现以及一些很好的示例。我用Tornado WebSocket服务器测试了以下内容,它可以正常工作。

from twisted.internet import reactor
from autobahn.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS


class EchoClientProtocol(WebSocketClientProtocol):

   def sendHello(self):
      self.sendMessage("Hello, world!")

   def onOpen(self):
      self.sendHello()

   def onMessage(self, msg, binary):
      print "Got echo: " + msg
      reactor.callLater(1, self.sendHello)


if __name__ == '__main__':

   factory = WebSocketClientFactory("ws://localhost:9000")
   factory.protocol = EchoClientProtocol
   connectWS(factory)
   reactor.run()

Autobahn has a good websocket client implementation for Python as well as some good examples. I tested the following with a Tornado WebSocket server and it worked.

from twisted.internet import reactor
from autobahn.websocket import WebSocketClientFactory, WebSocketClientProtocol, connectWS


class EchoClientProtocol(WebSocketClientProtocol):

   def sendHello(self):
      self.sendMessage("Hello, world!")

   def onOpen(self):
      self.sendHello()

   def onMessage(self, msg, binary):
      print "Got echo: " + msg
      reactor.callLater(1, self.sendHello)


if __name__ == '__main__':

   factory = WebSocketClientFactory("ws://localhost:9000")
   factory.protocol = EchoClientProtocol
   connectWS(factory)
   reactor.run()

回答 2

自从我最近在该领域进行了一些研究(Jan,’12)以来,最有希望的客户实际上是:WebSocket for Python。它支持可以这样调用的普通套接字:

ws = EchoClient('http://localhost:9000/ws')

clientThreaded或基于IOLoop龙卷风项目。这将允许您创建一个多并发连接客户端。如果要运行压力测试,则很有用。

客户端也暴露了onmessageopenedclosed方法。(WebSocket样式)。

Since I have been doing a bit of research in that field lately (Jan, ’12), the most promising client is actually : WebSocket for Python. It support a normal socket that you can call like this :

ws = EchoClient('http://localhost:9000/ws')

The client can be Threaded or based on IOLoop from Tornado project. This will allow you to create a multi concurrent connection client. Useful if you want to run stress tests.

The client also exposes the onmessage, opened and closed methods. (WebSocket style).


回答 3

web2py有comet_messaging.py,它使用Tornado进行websockets,请在此处查看示例:http : //vimeo.com/18399381和此处vimeo。com / 18232653

web2py has comet_messaging.py, which uses Tornado for websockets look at an example here: http://vimeo.com/18399381 and here vimeo . com / 18232653


回答 4

  1. http://code.google.com/p/pywebsocket/下查看回显客户端,这是一个Google项目。
  2. 在github中的一个很好的搜索是: https //github.com/search? = & = & = & =& =& =14& = & =1它返回客户端和服务器。
  3. Bret Taylor还通过Tornado(Python)实现了Web套接字。他的博客文章位于:Tornado中的Web套接字和客户端实现API,显示在客户端支持部分的tornado.websocket中。
  1. Take a look at the echo client under http://code.google.com/p/pywebsocket/ It’s a Google project.
  2. A good search in github is: https://github.com/search?type=Everything&language=python&q=websocket&repo=&langOverride=&x=14&y=29&start_value=1 it returns clients and servers.
  3. Bret Taylor also implemented web sockets over Tornado (Python). His blog post at: Web Sockets in Tornado and a client implementation API is shown at tornado.websocket in the client side support section.

Mitmproxy-面向渗透测试人员和软件开发人员的交互式TLS拦截HTTP代理

mitmproxy是一个交互式的、支持SSL/TLS的拦截代理,具有HTTP/1、HTTP/2和WebSocket的控制台接口

mitmdump是Mitmproxy的命令行版本。考虑HTTP的tcpdump

mitmweb是Mitmproxy的基于web的界面。

安装

安装说明如下here如果要从源代码安装,请参见CONTRIBUTING.md

文档和帮助

一般信息、教程和预编译的二进制文件可以在Mitmproxy网站上找到

Mitmproxy的文档可在我们的网站上找到:


如果您有关于如何使用Mitmproxy的问题,请在StackOverflow上询问他们!

贡献

作为一个开源项目,Mitmproxy欢迎各种形式的贡献

另外,请随时加入我们的开发者松弛!