How to get gevent websockets working? - python

I am using geventwebsockets but can't even get the example app to work. The code below gives me the error:
Traceback (most recent call last): File
"/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 884,
in handle_one_response
self.run_application() File "/usr/local/lib/python2.7/dist-packages/geventwebsocket/handler.py",
line 88, in run_application
return super(WebSocketHandler, self).run_application() File "/usr/local/lib/python2.7/dist-packages/gevent/pywsgi.py", line 870,
in run_application
self.result = self.application(self.environ, self.start_response) File
"/usr/local/lib/python2.7/dist-packages/geventwebsocket/resource.py",
line 90, in call
raise Exception("No apps defined") Exception: No apps defined
from geventwebsocket import WebSocketServer, WebSocketApplication, Resource
class EchoApplication(WebSocketApplication):
def on_open(self):
print "Connection opened"
def on_message(self, message):
self.ws.send(message)
def on_close(self, reason):
print reason
WebSocketServer(
('', 8000),
Resource({'/': EchoApplication})
).serve_forever()
I've been running around in circles. Can anyone help me out? Thanks so much.

Does it work for you
if you add the echo_app
def echo_app(environ, start_response):
websocket = environ.get("wsgi.websocket")
if websocket is None:
return http_handler(environ, start_response)
try:
while True:
message = websocket.receive()
websocket.send(message)
websocket.close()
except geventwebsocket.WebSocketError, ex:
print "{0}: {1}".format(ex.__class__.__name__, ex)
and
pass it like
print "Running %s from %s" % (agent, path)
WebSocketServer(("", 8000), echo_app, debug=False).serve_forever()
as shown in
https://bitbucket.org/noppo/gevent-websocket/src/0df192940acd288e8a8f6d2dd30329a3381c90f1/examples/echoserver.py

Related

Multiagent in Python using Spade localhost configuration issue

I want to try Multi Agent programming in Python using SPADE, but i can't seem to make this simple example work. The error refers to the server, so how can i set up the agents to work in the localhost ?
here is my code :
class SenderAgent(Agent):
class InformBehav(OneShotBehaviour):
async def run(self):
print("InformBehav running")
msg = Message(to="receiveragent#127.0.0.1") # Instantiate the message
msg.set_metadata("performative", "inform") # Set the "inform" FIPA performative
msg.body = "Hello World" # Set the message content
await self.send(msg)
print("Message sent!")
# stop agent from behaviour
await self.agent.stop()
async def setup(self):
print("SenderAgent started")
b = self.InformBehav()
self.add_behaviour(b)
class ReceiverAgent(Agent):
class RecvBehav(OneShotBehaviour):
async def run(self):
print("RecvBehav running")
msg = await self.receive(timeout=10) # wait for a message for 10 seconds
if msg:
print("Message received with content: {}".format(msg.body))
else:
print("Did not received any message after 10 seconds")
# stop agent from behaviour
await self.agent.stop()
async def setup(self):
print("ReceiverAgent started")
b = self.RecvBehav()
template = Template()
template.set_metadata("performative", "inform")
self.add_behaviour(b, template)
if __name__ == "__main__":
receiveragent = ReceiverAgent("receiveragent#127.0.0.1", '1234')
future = receiveragent.start()
future.result() # wait for receiver agent to be prepared.
senderagent = SenderAgent("senderagent#127.0.0.1", '1234')
senderagent.start()
while receiveragent.is_alive():
try:
time.sleep(1)
except KeyboardInterrupt:
senderagent.stop()
receiveragent.stop()
break
print("Agents finished")
as i'm getting the following errors :
connection failed: [Errno 10061] Connect call failed ('127.0.0.1', 5222)
Traceback (most recent call last):
File "C:/Users/Administrateur/PycharmProjects/spade/test.py", line 53, in <module>
future.result() # wait for receiver agent to be prepared.
File "C:\Users\Administrateur\AppData\Local\Programs\Python\Python37\lib\concurrent\futures\_base.py", line 435, in result
return self.__get_result()
File "C:\Users\Administrateur\AppData\Local\Programs\Python\Python37\lib\concurrent\futures\_base.py", line 384, in __get_result
raise self._exception
File "C:\Users\Administrateur\AppData\Local\Programs\Python\Python37\lib\site-packages\spade\agent.py", line 100, in _async_start
await self._async_register()
File "C:\Users\Administrateur\AppData\Local\Programs\Python\Python37\lib\site-packages\spade\agent.py", line 142, in _async_register
_, stream, features = await aioxmpp.node.connect_xmlstream(self.jid, metadata, loop=self.loop)
File "C:\Users\Administrateur\AppData\Local\Programs\Python\Python37\lib\site-packages\aioxmpp\node.py", line 415, in connect_xmlstream
exceptions
aioxmpp.errors.MultiOSError: failed to connect to XMPP domain '127.0.0.1': multiple errors: [Errno 10061] Connect call failed ('127.0.0.1', 5222)

int treated as `self' in python3

I want to build a simple server that dispatches requests depending on their path. So I wrote the following code:
from http.server import HTTPServer, BaseHTTPRequestHandler
import json
import socket
class Handler:
"""处理对应资源的请求"""
def __init__(self, dispatcher):
self.dispatcher = dispatcher
def handle(self):
"""根据dispatcher解析出来的path,调用对应的方法"""
command = 'do_' + self.dispatcher.command
print(command)
if not hasattr(self, command):
return;
method = getattr(self, command)
method(self)
def do_GET(self):
response = {
'message': 'message'
}
dispatcher.protocol_version = 'HTTP/1.1'
dispatcher.send_response(200, 'OK')
dispatcher.send_header('Content-type', 'text/plain')
dispatcher.wfile.write(bytes(json.dumps(response), 'utf-8'))
class dispatcher(BaseHTTPRequestHandler):
"""
根据url的不同分发请求
"""
def handle_one_request(self):
try:
self.raw_requestline = self.rfile.readline(65537)
if len(self.raw_requestline) > 65536:
self.requestline = ''
self.request_version = ''
self.command = ''
self.send_error(HTTPStatus.REQUEST_URI_TOO_LONG)
return
if not self.raw_requestline:
self.close_connection = True
return
if not self.parse_request():
# An error code has been sent, just exit
return
print(self.command)
print(self.path)
if self.path.startswith('/wrong'):
Handler(self).handle()
self.wfile.flush() #actually send the response if not already done.
except socket.timeout as e:
#a read or a write timed out. Discard this connection
self.log_error("Request timed out: %r", e)
self.close_connection = True
return
if __name__ == '__main__':
server = HTTPServer(('', 8080), dispatcher)
server.serve_forever()
The dispatcher will parse the incoming request and get the path so that it can decide which handler to call(though there is only one here for now).
In the handler class, it will call corresponding method based on the http method. In the do_GET method, it will call some methods in the dispatcher and that's where things go wrong.
When I ran this program and execute curl http://localhost:8080/wrong, I had the following exception:
GET
/wrong
do_GET
----------------------------------------
Exception happened during processing of request from ('127.0.0.1', 50072)
Traceback (most recent call last):
File "/usr/lib/python3.5/socketserver.py", line 313, in _handle_request_noblock
self.process_request(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 341, in process_request
self.finish_request(request, client_address)
File "/usr/lib/python3.5/socketserver.py", line 354, in finish_request
self.RequestHandlerClass(request, client_address, self)
File "/usr/lib/python3.5/socketserver.py", line 681, in __init__
self.handle()
File "/usr/lib/python3.5/http/server.py", line 422, in handle
self.handle_one_request()
File "hello.py", line 51, in handle_one_request
Handler(self).handle()
File "hello.py", line 18, in handle
method()
File "hello.py", line 25, in do_GET
dispatcher.send_response(200, 'OK')
File "/usr/lib/python3.5/http/server.py", line 487, in send_response
self.log_request(code)
AttributeError: 'int' object has no attribute 'log_request'
----------------------------------------
log_request is defined as follows in the super class of dispatcher:
def log_request(self, code='-', size='-'):
# blablabla
and it is called in dispatcher.send_response.
I guess the problem is that the 200 is treated as self by the interpreter.
If my guess is right, why is this happening?
If not, what causes this exception?
I know this question is still in the grammar level, but I'd appreciate it if someone can help me.

builtins.TypeError: 'NoneType' object is not callable in Twisted

I encountered a TypeError when I used twisted to create a TCP server.
When I run the client.py after starting server.py, I receive the error presented below in the Error traceback section.
I have no idea about this error. However, I am sure the error happens at server.py.
server.py:
from twisted.internet import protocol, reactor
from time import ctime
PORT = 1234
class TserverProtocols(protocol.Protocol):
def connectionMade(self):
clnt = self.clnt = self.transport.getPeer().host
print('...connected from:', clnt)
def dataReceived(self, data):
self.transport.write(('[%s] %s' %(ctime(),data)).encode())
factory = protocol.Factory()
factory.protocols = TserverProtocols
print('waiting for connetion...')
reactor.listenTCP(PORT, factory)
reactor.run()
client.py:
from twisted.internet import protocol, reactor
HOST = 'localhost'
PORT = 1234
class TSClntProtocol(protocol.Protocol):
def sendData(self):
data = input('>')
if data:
print('...sending %s' % data)
self.transport.write(data.encode())
else:
self.transport.loseConnetion()
def connectionMade(self):
self.sendData()
def dataReceived(self, data):
print(data)
self.sendData()
class TSCFactorty(protocol.ClientFactory):
protocol = TSClntProtocol
clientConnetionLost = clientConnectionFailed = lambda slef, connector, reason:reactor.stop()
reactor.connectTCP(HOST, PORT, TSCFactorty())
reactor.run()
Error traceback:
Unhandled Error
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/twisted/python/log.py", line 86, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/local/lib/python3.5/dist-packages/twisted/python/context.py", line 122, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/local/lib/python3.5/dist-packages/twisted/python/context.py", line 85, in callWithContext
return func(*args,**kw)
File "/usr/local/lib/python3.5/dist-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
--- <exception caught here> ---
File "/usr/local/lib/python3.5/dist-packages/twisted/internet/tcp.py", line 1069, in doRead
protocol = self.factory.buildProtocol(self._buildAddr(addr))
File "/usr/local/lib/python3.5/dist-packages/twisted/internet/protocol.py", line 133, in buildProtocol
p = self.protocol()
builtins.TypeError: 'NoneType' object is not callable
You have several typos in your code. Regarding your question, you should replace in server.py
factory.protocols = TserverProtocols
with
factory.protocol = TserverProtocols

Twisted: Mixing ReconnectingClientFactory with SSL write() raise ssl error frequently

I'm inheriting ReconnectingClientFactory and use SSL connection. We have it all running, but I am getting the following error after a few minute. Once this error happend, the connection is lost
Unhandled Error
Traceback (most recent call last):
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/python/log.py", line 103, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/python/log.py", line 86, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
--- <exception caught here> ---
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/internet/posixbase.py", line 597, in _doReadOrWrite
why = selectable.doRead()
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/internet/tcp.py", line 208, in doRead
return self._dataReceived(data)
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/internet/tcp.py", line 214, in _dataReceived
rval = self.protocol.dataReceived(data)
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/protocols/tls.py", line 430, in dataReceived
self._flushReceiveBIO()
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/protocols/tls.py", line 400, in _flushReceiveBIO
self._flushSendBIO()
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/twisted/protocols/tls.py", line 352, in _flushSendBIO
bytes = self._tlsConnection.bio_read(2 ** 15)
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1384, in bio_read
self._handle_bio_errors(self._from_ssl, result)
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/OpenSSL/SSL.py", line 1365, in _handle_bio_errors
_raise_current_error()
File "/usr/local/hikvision/ezops/python/lib/python2.7/site-packages/OpenSSL/_util.py", line 48, in exception_from_error_queue
raise exception_type(errors)
OpenSSL.SSL.Error: []
my client code like this:
reactor.connectSSL(opscenter_addr, int(opscenter_port), NoahAgentFactory(), ssl.ClientContextFactory())
class NoahAgentFactory like this:
import logging
import traceback
from OpenSSL import SSL
from twisted.internet import reactor,ssl
from twisted.internet import task
from twisted.internet.protocol import Protocol
from twisted.internet.protocol import ReconnectingClientFactory
import DataPackageCodecFactory as Codec
from Header import MessageHandlerMap
from Handler import HBReq, DefaultRspHandler
from Util import Connection
logger = logging.getLogger('opsagent')
class NoahAgentProtocol(Protocol):
t = None
def connectionMade(self):
'''
客户端连接成功之后会自动调用该方法
:return:
'''
self.transport.setTcpKeepAlive(True) # maintain the TCP connection
self.transport.setTcpNoDelay(True) # allow Nagle algorithm
# 连接成功后保存连接信息
Connection.AgentTcpConnection = self
global t
logger.info('is already connect to the server')
self.recv_data_buffer = ''
# 创建定时的心跳任务,每隔30秒执行一次
t = task.LoopingCall(HBReq.execute, *[self])
t.start(30)
def dataReceived(self, data):
logger.debug("Received Message: %s", repr(data))
###code handler packages##########
def connectionLost(self, reason):
'''
当客户端连接断开的时候,会自动调用该方法
:param reason:
:return:
'''
try:
t.stop()
# 清空全局链接信息
Connection.AgentTcpConnection = None
except:
logger.error(traceback.format_exc())
logger.info('Connection is lost and Task stopped,Resaon =>%s', reason)
class NoahAgentFactory(ReconnectingClientFactory):
def startedConnecting(self, connector):
logger.info('Started to connect.')
def buildProtocol(self, addr):
logger.info('Connected.')
logger.info('Resetting reconnection delay')
self.resetDelay()
return NoahAgentProtocol()
def clientConnectionLost(self, connector, reason):
logger.info('Lost connection. Reason:%s', reason)
self.resetDelay()
self.retry(connector)
# ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
def clientConnectionFailed(self, connector, reason):
logger.info('Connection failed. Reason:%s', reason)
self.resetDelay()
self.retry(connector)
I think it is not a bug in Twisted's TLS other than a mistake,I send the message not in the main reactor thread,as we know,twisted is single thread mode,so there is no need to consider thread safety issues.But I use reactor.callInThread to deal long time cost bussiness and send message directly,and the write() is not thread safety,so invoke that error,once I use reactor.callFromThread on send message,it's running right.

Unable to write into file using Twisted

I have this code snippet which I am using to establish a connection with a client and then writing all his text to a file. Here is the code
from twisted.internet.protocol import Protocol, Factory
from twisted.internet import reactor
import ctypes # An included library with Python install.
class DataTransfer(Protocol):
def connectionMade(self):
#self.transport.write("""connected""")
fwrite = open('/Applications/mamp/htdocs/datatest.txt','r+')
self.factory.clients.append(self)
print "clients are ", self.factory.clients
self.username = ""
self.password = ""
self.auth = False
self.ipaddress = self.transport.getPeer()
print self.ipaddress
def connectionLost(self, reason):
fwrite.close()
self.factory.clients.remove(self)
print reason
def dataReceived(self, data):
print data
fwrite.write(data)
a = data.split(':')
if len(a) > 1:
command = a[0]
content = a[1]
msg = ""
self.message(msg)
def message(self, message):
self.transport.write(message + '\n')
factory = Factory()
factory.protocol = DataTransfer
factory.clients = []
reactor.listenTCP(8889, factory)
print "Server started"
reactor.run()
I am executing this command pythonw socketListner.py. The server starts successfully. But when I connect to it using netcat command, I get the following in my terminal and the connection closes.
Unhandled Error
Traceback (most recent call last):
File "socketListner.py", line 42, in <module>
reactor.run()
File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 1194, in run
self.mainLoop()
File "/Library/Python/2.7/site-packages/twisted/internet/base.py", line 1206, in mainLoop
self.doIteration(t)
File "/Library/Python/2.7/site-packages/twisted/internet/selectreactor.py", line 143, in doSelect
_logrun(selectable, _drdw, selectable, method)
--- <exception caught here> ---
File "/Library/Python/2.7/site-packages/twisted/python/log.py", line 101, in callWithLogger
return callWithContext({"system": lp}, func, *args, **kw)
File "/Library/Python/2.7/site-packages/twisted/python/log.py", line 84, in callWithContext
return context.call({ILogContext: newCtx}, func, *args, **kw)
File "/Library/Python/2.7/site-packages/twisted/python/context.py", line 118, in callWithContext
return self.currentContext().callWithContext(ctx, func, *args, **kw)
File "/Library/Python/2.7/site-packages/twisted/python/context.py", line 81, in callWithContext
return func(*args,**kw)
File "/Library/Python/2.7/site-packages/twisted/internet/selectreactor.py", line 154, in _doReadOrWrite
self._disconnectSelectable(selectable, why, method=="doRead")
File "/Library/Python/2.7/site-packages/twisted/internet/posixbase.py", line 258, in _disconnectSelectable
selectable.connectionLost(failure.Failure(why))
File "/Library/Python/2.7/site-packages/twisted/internet/tcp.py", line 293, in connectionLost
protocol.connectionLost(reason)
File "socketListner.py", line 17, in connectionLost
fwrite.close()
exceptions.NameError: global name 'fwrite' is not defined
Your variable fwrite is defined locally in connectionMade. Try to make it an instance variable by defining it as self.fwrite everywhere you use it within your class.

Categories

Resources