I am desktop programmer but I want to learn something about web services. I decided for python. I am trying understand how web applications works. I know how to create basic tornado website (request - response) and working jabber client, but I don't know how to mix them. Can I use any python components in web services? Does they must have specific structure ( sync or async )? Because I'm stuck in loop handlers:
If tornado start web serwer by command:
app = Application()
app.listen(options.port)
tornado.ioloop.IOLoop.instance().start()
... so how (where) can I start xmpp loop?
client.connect()
client.run()
I think that tornado listen loop should handle xmpp listening, but don't know how
Regards.
Edit: I forgot. I am using pyxmpp2
I believe what you are trying to accomplish is not feasible in one thread of python as both are trying to listen at the same time which isn't possible in one thread. Might I suggest looking at this tutorial on threading.
Another question would be are you trying to make a web based xmpp or just have a xmpp & html server running in the same script. If you wish to try the former I would advise you to look into inter-thread communication either with zeromq or queue
maybe WebSocketHandler and Thread will help you.
Demo
class BotThread(threading.Thread):
def __init__(self,my_jid,settings,on_message):
super(BotThread,self).__init__()
#EchoBot is pyxmpp2's Client
self.bot = EchoBot(my_jid, settings,on_message= on_message)
def run(self):
self.bot.run()
class ChatSocketHandler(tornado.websocket.WebSocketHandler):
def open(self):
#init xmpp client
my_jid =
settings =
bot =BotThread(my_jid, settings,on_message=self.on_message)
bot.start()
Related
I am trying to find an example of a python aiohttp app that both makes http requests as a client AND processes http requests as a server.
I need to write an app that retrieves data from an internet web server AND responds to requests for the re-formatted and re-packaged data.
Could someone reference an example or provide an outline of how to structure such an app using aiohttp?
One issue I am having trouble figuring out is how to combine the loop.run_until_complete() used to start a client that is making requests and web.run_app() that is used to start a server since each of these is a blocking call.
Somehow I need to start them both.
I thought web.AppRunner() might help me start the server in a non-blocking manner, and I came across the following example in the aiohttp documentation:
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, 'localhost', 8080)
await site.start()
while True:
await asyncio.sleep(3600) # sleep forever
I thought I could use something like this to start the server and then also start the client. But I can't seem to call this from anything that isn't itself an async function (await outside async function).
I could also use some advice on how to share the data between the client process and the server process as the client need to periodically refresh the data from the internet and the server needs to be able to continuously serve up the latest version of the data. Presumably I need some sort of locking mechanism while the client is updating the data resource shared between the client process and server process?
First important point for me: I want to implement websockets. I do not need the fallback options of socketIO.
I would like "my clients" to implement whatever they want, as soon as they stay to the websockets protocol. Namely something like: var ws = new WebSocket.
So.. if the server is Flask-SocketIO, will a simple js WebSocket work?
ADDIONAL NOTES:
Python first!
I am trying to set up a server which will respond only (actually only send) to websockets, no web page associated. (Yes, I am fine with WS and I do not need WSS, in case you ask ;) ).
I had a try on the server side with flask-sockets
https://github.com/kennethreitz/flask-sockets
but it is giving me some problems. Like closing immediately the connection and
beside many similar problems on the web I could not find a solution. Hard to debug too. So before I start developing a new server...
Sadly no, you cannot use a Socket.IO server with plain WebSocket clients. Sorry, that's not what Flask-SocketIO was made for.
(in case this isn't clear, this is the author of Flask-SocketIO speaking)
I'm trying to integrate a RESTful responder in a Crossbar application, for which the best fit seems to be a WSGI service. This service ideally should be part of the rest of the pub/sub infrastructure, being able to receive WAMP events on the one hand and answer HTTP requests on the other.
The difficulty is to run an event loop which allows asynchronous web socket events and additionally offer a WSGI compliant component. It seems to me that Pulsar should be able to do that, but I have not been able to figure out how to set it up, none of the available samples demonstrate exactly this use case.
value = None
class Foo(ApplicationSession):
def onJoin(self, details):
yield self.subscribe(self.bar, 'bar')
def bar(self, data):
value = data
app = Flask(__name__)
#app.route('/')
def baz():
return value
if __name__ == '__main__':
runner = ApplicationRunner('ws://127.0.0.1:8080', 'test')
runner.run(Foo, start_reactor=False)
# now what?
The above demonstrates the two parts, an Autobahn WAMP client and a Flask WSGI component. How do I run both of these in parallel, allowing one thread to receive events both via HTTP and web socket? I don't particularly care about the version of Python nor underlying library (Twisted, asyncio, Pulsar, Flask), I'd just like to get this running somehow.
WSGI is an inherently synchronous API. I don't know about Pulsar, but I would be surprised if it could somehow magically work around this fact.
The way Crossbar.io integrates with classic Web (and synchronous) stacks is via a REST-bridge. Currently, we have the WAMP "Publisher" role covered today (2015/02): that is, you can publish an WAMP event by doing a simple HTTP/POST http://crossbar.io/docs/HTTP-Pusher-Service/. This REST bridge in Crossbar.io will be extended to cover all 4 WAMP roles in the near future.
If you take a step back, and primarily care about something do create a REST API in your app, and which integrates directly with WAMP and asynchronous stuff, I'd have a look a Twisted Klein. Twisted Klein is essentially modeled after Flask, but at the source level. We have a blog post that covers exactly this: Mixing Web and WAMP code with Twisted Klein
I recently discovered autobahn python and js as a comfortable method to establish a pub/sub server and corresponding client even with rpc-calls.
After looking through the tutorials, I set up a test version with a websocket server and a webserver running on the same port. The server sends periodically data to the client via websockets. The html the user gets lies on the localhost root. All that works fine.
However, what I want to accomplish is: Setup a pub/sub server and a webserver listening on the same port.
The tutorials show only how to setup these on two different ports (as shown at http://autobahn.ws/python/tutorials/pubsub).
Im very new to python in general and autobahn and twisted especially.
Any advice would be really nice!
Thanks very much!
Marc
Sure. You can run a WAMP/WebSocket server and a plain old Web server on one port using Autobahn. Here is an example for pure WebSocket and here is one for WAMP.
Disclaimer: I am author of Autobahn and work for Tavendo.
When using WAMP while having HTTP and WS servers listening on the same port you will need to start your instance of WampServerFactory manually as explained here.
factory = WampServerFactory("ws://localhost:8080")
factory.protocol = YourServerProtocolClass
factory.startFactory() # <--- need to call this manually
resource = WebSocketResource(factory)
root = File(".")
root.putChild("ws", resource)
For more details please see this complete example.
I would put nginx as a frontend that forwards each call either to pubsub or to web... Recent Nginx supports WebSocket forwarding.
Or you man write something similar with Twisted :)
Another alternative would be to adapt autobahn.websocket.WebSocketServerProtocol and its subclass autobahn.wamp.WampServerProtocol to Twisted.web. It should be possible.
I'm trying to implement long pulling client in Tornado, that interacts with an asynchronous Tornado server.
What happens is one of 2 things:
Either the client timesout, or
The client receives all the messages at once after finishing the
whole background process, similar to blocking ones
This is the client I use:
from tornado import ioloop
from tornado import httpclient
print "\nNon-Blocking AsyncHTTPClient"
import tornado.ioloop
def async_call(response):
if response.error:
response.rethrow()
print "AsyncHTTPClient Response"
ioloop.IOLoop.instance().stop()
http_client = httpclient.AsyncHTTPClient()
http_client.fetch("http://localhost:9999/text/", async_call)
ioloop.IOLoop.instance().start()
Is this the right way to write a long-polling/comet client?
I would also appreciate for those who will answer to provide a sample async-server in Tornado, because may be I'm writing the cometed Tornado server wrongly... I'm a bit new to the whole long-polling process in general.
Tornado itself has an excellent example of chat, built on top of long-polling mechanism
https://github.com/facebook/tornado/tree/master/demos/chat
It helped me a lot to understand everything, and it have both server and client.