Keyboard input using stdio.StandardIO in twisted python - python

I have a doubt in twisted python related to my multiclient chat server program.
that is, when we give input from keyboard using stdio.StandardIO, where it is stored when we run the reactor? Can anybody give me the answer, please..

twisted.internet.stdio.StandardIO doesn't "store" data anywhere. It is a transport which you associate with a protocol. The protocol you associate with it can do anything you want with the data delivered to it.
You can find two examples of using StandardIO in the Twisted documentation, https://twistedmatrix.com/documents/current/_downloads/stdin.py and https://twistedmatrix.com/documents/current/_downloads/stdiodemo.py.

Related

real time communication between python server and a python client(not a browser)

Back in the javascript world there is a library all the socket part called socket.io
the way it works is by send and receiving the data by and events.
for example: the server can send to the client an event called "msg_received" with the message itself as the data.
in python it seems to not be as asynchronous as its in javascript. its also not working with events but only with strings which is a bit diffrent than how i use to work with the sockets.
i.e socket.send(<string>), socket.recv(<integer>)
i would love if anyone could put some light the issue to explain to me how it works in python since i've searched all over the internet and couldn't find an answer.. or point me to a similar library like the one i mentioned above.

How to communicate between client device and webserver?

I've build a little device based on the raspberry pi. Now I want to configure it using my web server. The idea is that I enter all the details on my django web page and then the device just pulls that off the server.
But there are two problems I'm not sure how to solve:
I have multiple devices for multiple users so some kind of Login must be provided.
The device also sends pictures from time to time. Right now it's using FTP with a general login, but I want to personalize that too for every device. The uploads will need a resume function so http is out!
So the basic question is: Should I get started with sockets or is there a better and safer way to do it? Maybe there is some kind of open source library that's been tested a lot?
Instead of hand coding sockets, I would suggest using HTTP with BASIC authentication to communicate between the device and the web server. You can uniquely assign an id/pwd to each device, and BASIC authentication is well supported by all web servers and client side libraries.
There are some security concerns with BASIC authentication even if you use HTTPS, but it maybe acceptable in your particular case here.
Maybe you could use SSH, with Fabric for instance. Here an example.

Correct way to code a Python Server

Which way is better?
Creating a while loop and then using the select module OR using ThreadedTCPServer with a custom class.
Im having problems with the Threaded TCP Server, although it could just be my coding.
My personal recommendation is to use Twisted. It's a Python-based framework intended primarily for writing event-driven network software. The documentation has a lot of great examples of how to create various types of servers and clients, as well.
I am sure there is no such a thing like the "correct" way.
If you want not, must not or cannot use any of the existing server implementations the general idea is (in pseudo code):
ss = serversocket()
ss.bind ()
while (True):
cs = ss.accept ()
spawnCommThread (cs)
In the CommThread for each client you take care of reading from the socket returned by accept, communicate with your client and die, when the client closes the connection or another criterion is given.

Python inter-computer communication

This whole topic is way out of my depth, so forgive my imprecise question, but I have two computers both connected to one LAN.
What I want is to be able to communicate one string between the two, by running a python script on the first (the host) where the string will originate, and a second on the client computer to retrieve the string.
What is the most efficient way for an inexperienced programmer like me to achieve this?
First, lets get the nomenclature straight. Usually the part that initiate the communication is the client, the parts that is waiting for a connection is a server, which then will receive the data from the client and generate a response. From your question, the "host" is the client and the "client" seems to be the server.
Then you have to decide how to transfer the data. You can use straight sockets, in which case you can use SocketServer, or you can rely on an existing protocol, like HTTP or XML-RPC, in which case you will find ready to use library packages with plenty of examples (e.g. xmlrpclib and SimpleXMLRPCServer)
There are about a million ways.
If I were doing it, I'd use the SocketServer library, because it's not too insane, fairly well documented, and most importantly, I've used it before.
There are a couple of examples here: http://docs.python.org/library/socketserver.html#examples
File share and polling filesystem every minute. No joke. Of course, it depends on what are requirements for your applications and what lag is acceptable but in practice using file shares is quite common.

How do I go about writing a program to send and receive sms using python?

I have looked all over the net for a good library to use in sending and receiving sms's using python but all in vain!
Are there GSM libraries for python out there?
Have you looked at py-sms?
Python-binding for Gammu perhaps?
BTW. It's GUI version (Wammu) is written in wxPython.
I have recently written some code for interacting with Huawei 3G USB modems in python.
My initial prototype used pyserial directly, and then my production code used Twisted's Serial support so I can access the modem asynchronously.
I found that by accessing the modem programatically using the serial port I was able to access all the functionality required to send and receive SMS messages using Hayes AT commands and extensions to the AT command set.
This is the one of the referneces I was able to find on the topic, it lists these commands:
AT+CMGL List Messages
AT+CMGR Read message
AT+CMGS Send message
AT+CMGW Write message to memory
They are complicated commands and take arguments and you have to parse the results yourself.
There are more references on the internet you can google for that reference these 4 commands that will let you work out how your modem works.
I provided a detailed answer to a similar question here. I mention a Python interface to an SMS gateway provided by TextMagic.
Also check PyKannel.
There's also a web service for sending and receiving SMS (non-free, of course), but I don't have URL at hand. :(
I was at a hackday where we had free access to a few APIs, including twillio. It was very easy to use their python library to send and receive SMS, and we built an app around it in about 3 hours (twillio integration was about 15 minutes).

Categories