How to send a request by a private protocol with Python - python

I want to send a request to a server which is a private protocol based on TCP (Not HTTP), How Can I send a request using Python?

Python sockets are what you are looking for. Take a look at the Python Socket class at https://docs.python.org/2/library/socket.html
This site includes examples of how to set up a server and client and provides a basic example of the fundamental TCP Communication using Python. If you need something with more control you may want to look at Scapy: http://www.secdev.org/projects/scapy/
Scapy is a python implementation that provides a framework to control almost all aspects to Network Communication all the way down to Layer 2 (Ethernet Frame).

Related

Communicating over a local server in python

I am creating a colloabrative note-making app in python.
Here, one guy on computer running the app can create the server subseuqently the changes on the screen([color, pixel], where pixel=[x,y]) will be transmitted to others connected to the server.
I am using kivy for creating the app. My question is with respect to transmitting the data over the server.
I can create server using this:
import socket
ip_address=socket.gethostbyname(socket.gethostname())
execfile( "manage.py runserver "+ip_address+":8000" )
Now, how do others connect to the server and request the data(assuming the above code is correct). Also, how to send the data in django.
Well, Django is a framework that allows creating a site or API that is reachable through HTTP protocol. This has several consequences for you:
Server cannot send a message to client unless the client asks. HTTP is a "request-response" protocol. Client sends a request (for example, http://server.com/getUpdates?id=100500) and gets a response from server.
Creating clients that ask the server to give them updates all the time is a bad practice, probably leading to server DoS.
Although you can use WebSockets, using Django for such a task is really an overkill.
Summarizing, you need a reliable duplex channel for sending data in both directions. I'd start with TCP server, rather than HTTP. Fortunately, Python stdlib has a module you can start with - socketserver.
Additional reading
TCP
UDP (you will probably want this for broadcasting)
Berkeley sockets (a socket standard underlying socketserver module)
TCP vs. UDP
When deciding what protocol to use, following aspects should be considered:
TCP is reliable. Messages never disappear implicitly. If there was a network error, message will be resent. If there's no connection, explicit error will be raised. TCP uses several algorithms to fit into the network channel. It is an intelligent protocol.
UDP is unreliable. It possesses no feature TCP has. Packets can disappear, get reordered. But UDP messages are lightweight and in experienced hands they summon to life such systems as network action games and streaming video (lost and reordered messages aren't crucial here and TCP becomes too slow).
So I'd recommend to start with TCP. It's way more easier to get working fast and correct than UDP. Switch to UDP if you have some experience with TCP and there are a lot of people using you app and wanting to get the lowest latency possible.

Python JSON-RPC_2.0 TCP Server Client Explained

I'm having a difficult time fully understanding the nature of a TCP server/client relationship when a JSON string is sent to the server. The information I need may be out there, but I'm perhpas not using the correct search paramaters as I'm looking.
I've built a Python TCP, JSON-RPC Server from the following examples:
https://github.com/joshmarshall/jsonrpclib
http://code.activestate.com/recipes/552751-json-rpc-server-and-client/
In both cases, I can communicate with the Python server from a Python console on a different computer, sending commands from one (the client) to the other (server). In all of the examples, I've had to install the libraries mentioned above on both the client and the server machines in order to facilitate the TCP communication.
So the background to my situation and question is, when does JSON enter the mix? This is what I want to do:
Setup a Python TCP server that accepts a JSON string from a remote client inside (or outside) the network. The server parses the JSON string, fetches the method and parameters from the objectified string, and executes the method. The server then sends a JSON string result to the calling client. In this case, the client is a mobile application (iPad, Android, etc) with a JavaScript library that I'll use to send the requests to the server.
Why would I need a Python client? From what I can gather, the client just needs to open a connection to the server and then send the JSON string, right? Why do all the code samples include Python client examples? Are they assuming a server machine is going to talk to a server machine, so they have included client code to help generate the JSON string that will be sent to the server?
If I assume that a Python client isn't really needed for anything, I've been sending JSON strings to the Python server from the iPad, but in each case the server is reporting a "Bad request syntax" error. I'll pen a new question on that issue if I'm understanding the current question correctly.
Insight is appreciated.
The JSON encoding is the lingua franca of your RPC protocol, so you can indeed use any client you like. The implementations you found for JSON-RPC use the HTTP protocol, a very specific communication protocol built on top of TCP/IP, but you can implement the same protocol over raw TCP-IP sockets if so required.
The examples include both the Python client and the server because they illustrate how to implement the JSON-RPC standard in Python, not in JavaScript or C or Lisp. They focus on the implementation in one language. The JSON-RPC standard however, is language agnostic. It doesn't matter what language you write either the server or the client in, as long as they use the same standard.

How to create a communication between browser and application

I have a big problem, and I am having a hard time solving it. I have a custom made game controller, which outputs some data from it's sensors via serial communication and is connected to PC via serial port. I do the callculation of the current controller position in a Matlab script. I am building a web application that will display the data (position) of the device in a web browser, but can't seem to work out, how to connect my device to the browser. Matlab script sends all the position data to a UDP port with a sampling fequency of 100HZ (100 samples per second). I need to make a persistent connection between a web browser and my matlab script so I will be able to display the data. I am thinking about using web sockets API. but it does not "speak" UDP. So my idea was to somehow read the data with from UDP with a custom Python server and then create a websocket on that Python server and send data received via UDP port to web browser. Oh, and it would be nice if I could communicate in both directions. Will this work? Any ideas on how to do it? How is this usually done, I mean how can one connect let's say some temperature sensor to web browser to display data in real time?
Any answer will be gladly appreciated.
Thanks,
Leon
Note that although the WebSockets protocol is built on TCP sockets, the WebSockets protocol is not raw TCP sockets. A WebSockets connection has an HTTP friendly handshake (with some CORS functionality built-in). WebSockets are also message based (rather than streaming like TCP) so each message has a couple of bytes of framing headers.
You might look at websockify (disclaimer: I made websockify). Websockify is a python server that bridges/proxies between WebSockets and plain TCP sockets. I don't think it would be particularly difficult to adapt it to handle UDP sockets on the backend.
WebSockify (designed to be used together with the included include/websock.js front-end library) supports binary data even over the older Hixie versions of the protocol. This allows it to work with iOS (iPhone,iPad) devices which still only support the older version of the protocol.

Implementing and Designing a protocol for client server application

I'm just investigating about implementing and designing a protocol, but I'm stucked on how to start!
Let's take an example the LDAP protocol, I have the rfc and I have the asn1 grammar specification.
Now How can I start developing a server in a such a way that a client compliant to the RFC LDAp can communicate with me without having any kind of problem?
LDAP it's just an example, I can implement FTP, or custom protocol.
But I want to be sure that I am standard for any client once the protocol is defined, even if the implementation is different.
So which is the way of communication?
Can you give some good starting points?
Thanks
P.S: I would like to implement the server in python but that's not important according what I said.
Start with an asn.1 library. Then make a server that does what the RFC says to do. Protocols are like APIs, only you have to implement the guts instead of the wrapper. You communicate LDAP (or FTP) messages over a transport protocol like TCP. If you need help making a server, you might want to look into twisted.

Python, implementing proxy support for a socket based application (not urllib2)

I am little stumped: I have a simple messenger client program (pure python, sockets), and I wanted to add proxy support (http/s, socks), however I am a little confused on how to go about it. I am assuming that the connection on the socket level will be done to the proxy server, at which point the headers should contain a CONNECT + destination IP (of the chat server) and authentication, (if proxy requires so), however the rest is a little beyond me. How is the subsequent connection handled, specifically the reading/writing, etc...
Are there any guides on proxy support implementation for socket based (tcp) programming in Python?
Thank you
Maybe use something like SocksiPy which does all the protocol details for you and would let you connect through a SOCKS proxy as you would without it?
It is pretty simple - after you send the HTTP request: CONNECT example.com:1234 HTTP/1.0\r\nHost: example.com:1234\r\n<additional headers incl. authentication>\r\n\r\n, the server responds with HTTP/1.0 200 Connection established\r\n\r\n and then (after the double line ends) you can communicate just as you would communicate with example.com port 1234 without the proxy (as I understand you already have the client-server communication part done).
Have a look at stunnel.
Stunnel can allow you to secure
non-SSL aware daemons and protocols
(like POP, IMAP, LDAP, etc) by having
Stunnel provide the encryption,
requiring no changes to the daemon's
code

Categories