Twisted and PyBluez working together? - python

I want to use twisted and bluetooth together. At the moment I am doing this with PyBluez running in an twisted thread.
PyBluez just creates some socket (or socket-like? it has a file descriptor like a normal socket) object, basically you do:
sock=bluetooth.BluetoothSocket( bluetooth.RFCOMM )
sock.connect((device_id,1))
Can't I just insert that socket into the twisted reactor somehow and connect it with a Protocol?

You can write a class implementing IReadDescriptor (or IWriteDescriptor) and connect it to reactor, like in this example.

I found this project which combines pybluez with twisted: http://pydoc.net/airi/0.1.1/airi.twisted_bluetooth

This piece of code actually helped me a lot. I now have a working implementation in Twisted.

Related

How to establish a connection between two computers wirelessly?

Maybe is my question a little bit weird, but I am looking for an answer since couple days.
The only thing what I found were sockets and asyncio(dont really know what it is) , but with sockets you can establish a connection just in your localhost.
Is there any otherways to creat a connection between two quite different pcs or between a client and server wireless.
If you have any ideas just write, maybe it could be helpful. As a beginner in python, I would be happy if you guys could show me some way to find easy tutorials (maybe some websides) and important things to learn. So everything could be helpful, I am just curious. :)
You can create a server / client pair, through executables running on both PCs. TCP and UDP sockets can be used to communicate outside 'localhost'. You can do this rather easily in many programming languages like C, C#, python amongst others.
what you can do is create two seperate python files: client.py and server.py
The client.py will be trying making the connection. the server.py will be trying to receive that connection. You mentioned you weren't familiar with socket. Socket is a low-level networking interface when you import the library socket to a file, you are able to make system calls between different computers to interact with each other.
You need to install ros library form here for example if you have Ubuntu 16.04.
after that it is very easy to use those lines
We used this method with robots.

Python Simple Multiple Client Socket Server

I am wondering how I can make a simple socket server in Python 2.7 which can handle and add/accept multiple clients at a time. I do not want to use Twisted nor threading, nor any libraries; just Python, and sockets. I have looked around SoF (stackoverflow- is that a thing?) and found people asking the same question but not getting a clear answer.
If you are wondering why I need to do this, It's because I want to create a simple data forwarder which forwards client data to another server. I think a very simple example showing me Server.py, Client1.py, and Client2.py is just what I need. Again, just a very simple example with no threading, no twisted, no libraries.
I hope you can help me, I'm fairly new to Python and I feel like this project will help get me on my feet, and I learn great from examples.
Consider using asyncio (available for python 3.3 and later).
Asyncio is the new python standard for single-threaded concurrent programming:
This module provides infrastructure for writing single-threaded concurrent code using coroutines, multiplexing I/O access over sockets and other resources, running network clients and servers, and other related primitives.
The documentation provides a few examples:
TCP echo client
TCP echo server
If you're not ready to migrate to python 3, you can use trollius, the portage of asyncio for python 2. There is a few differences between the two modules, as listed in the documentation:
replace asyncio with trollius (or use import trollius as asyncio)
replace yield from ... with yield From(...)
replace yield from [] with yield From(None)
in coroutines, replace return res with raise Return(res)
Other solutions for single-threaded concurrent programming on python 2.7:
gevent: a coroutine-based Python networking library that uses greenlet.
asyncore: built-in asynchronous socket library (echo server example).

Inter-process communication between Python and Scala programs

I have a website that is backed by CherryPy web framework and a scala program that runs on a same machine and contains an actor system inside. OS is Ubuntu 12.04.
What I want is this: once user fills out and submits a form I send the data from CherryPy backend to scala program as a JSON string. How can this be done? What should I use in my python and scala programs to implement this functionality?
Instead of using raw sockets, you could consider a message broker like RabbitMQ. It supports both Scala and Python.
http://www.rabbitmq.com/tutorials/tutorial-one-python.html
On the Scala side, Akka has an AMQP module which abstracts AMQP Connection, Producer and Consumer as Actors.
http://doc.akka.io/docs/akka-modules/1.3.1/modules/amqp.html
As you are on a *nix system, you may want to look into Unix domain sockets (that link contains a very clear example use).
You can use the python socket module to easily create a Unix socket using:
import socket
sock = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)

Multiple reactors (main loops) in one application through threading (or alternative means)

I've got an idea for an app I'd like to work on to learn a bit more about Twisted and WebSockets. I was thinking of integrating a previously written IRC Bot into a web application. As far as I can see it, I would need three reactors to make it work:
Primary Reactor: Web Server (HTTP). This would be your average twisted.web application. When you access it, you can POST an IRC server/channel to connection. The web server would then talk to a different reactor in a different thread, which is...
Secondary Reactor: IRC Bot. This would be an IRC bot running via the Twisted IRC client protocol. It would join a channel, and whenever something was said, it would take that data and push it to yet another reactor, on yet another thread, which is...
Tertiary Reactor: WebSocket Server (WS): Since WebSockets don't use the regular HTTP Protocol, they need their own server (or so it seems, looking at examples such as this. When the IRC bot receives a message, it tells the WebSocket Server to push that message to connected clients.
In my mind, this makes sense. It seems like it would be possible. Does anyone have any examples of multiple reactors running in separate threads, or is this something I've imagined that can't be done in the current incarnation of Twisted.
Are there any architecture changes that can (or should) be made to minimize the reactor count, etc?
Thanks for any help.
Lucky for you, it is easy to reduce the number of reactors, specifically, to 1:
You can only ever have a single reactor, in a single thread, in any given Twisted process. If you try to have more, nothing will work.
The whole point of a reactor, actually, is to facilitate having multiple sources of events combined into a single thread. If you want to listen on 3 different ports with 3 different protocols, your application might look like this:
from twisted.internet import reactor
reactor.listenTCP(4321, FirstProtocolFactory())
reactor.listenTCP(5432, SecondProtocolFactory())
reactor.listenTCP(6543, ThirdProtocolFactory())
reactor.run()
Of course, you may not actually be calling listenTCP directly yourself, as you probably want to use Service objects from twisted.application.internet if you are using twistd, either via a .tac file or a twistd plugin. And you won't need to call reactor.run() yourself, if twistd is doing it for you. My point here is that, via whatever means, you load up the reactor with all the events you expect it to react to - listening servers, client connections, timed events - and it will react to each one as it occurs. (Hence, "reactor".)
For the specific values of what FirstProtocolFactory, SecondProtocolFactory, and ThirdProtocolFactory should be, see the links in pyfunc's answer.
No, I don't think you need multiple reactors.
What you need, is a multi-service multi-protocol application. This is where Twisted really shines.
So your application should start a web service, IRC Bot service and WebSocket server.
Use twisted application service framework, specially starting a multi service
http://twistedmatrix.com/documents/10.1.0/core/howto/application.html
http://twistedmatrix.com/documents/10.1.0/api/twisted.application.service.MultiService.html
Check out the IRC bot implementation and twisted IRC protocol support:
http://twistedmatrix.com/documents/10.1.0/api/twisted.words.protocols.irc.IRC.html
http://twistedmatrix.com/documents/current/words/
http://code.google.com/p/pyfibot/
and for websocket and twisted
http://twistedmatrix.com/trac/export/29073/branches/websocket-4173-2/doc/web/howto/websocket.xhtml

Non Blocking Server in Python

Can someone please tell how to write a Non-Blocking server code using the socket library alone.Thanks
Frankly, just don't (unless it's for an exercise). The Twisted Framework will do everything network-related for you, so you have to write only your protocol without caring about the transport layer. Writing socket code is not easy, so why not use code somebody else wrote and tested.
Why socket alone? It's so much simpler to use another standard library module, asyncore -- and if you can't, at the very least select!
If you're constrained by your homework's condition to only use socket, then I hope you can at least add threading (or multiprocessing), otherwise you're seriously out of luck -- you can make sockets with timeout, but juggling timing-out sockets without the needed help from any of the other obvious standard library modules (to support either async or threaded serving) is a serious mess indeed-y...;-).
Not sure what you mean by "socket library alone" - you surely will need other modules from the standard Python library.
The lowest level of non-blocking code is the select module. This allows you to have many simultaneous client connections, and reports which of them have input pending to process. So you select both the server (accept) socket, plus any client connections that you have already accepted. A thin layer on top of that is the asyncore module.
Use eventlets or gevent. It monkey patches existing libraries. socket module can be used without any changes. Though code appears synchronous, it executes asynchronously.
Example:
http://eventlet.net/doc/examples.html#socket-connect

Categories

Resources