Socket Programming when there is a path - python

For python
so if you do socket programming u would do a host and port e.g. localhost and 9999 respectively
what if instead of port u have a path e.g. localhost/foo
could u still program a server and client connection without a port number and a path instead e.g. localhost/foo
if not, then would http library work instead so like at the top instead of import socket u type import http and use the thing http library has instead of socket library
is there a way to do it with socket in python when u have a path like localhost/foo but no port i.e. u do not have a host and port like localhost and 9999

Let's take a look at this picture (found here):
Network protocols form a stack where the protocols above rely on the protocols below. For example, when you write a regular letter to your friend, all you care about is to write it in a proper language and write a correct address on the envelope. All the logistics details, whether your letter is delivered by an airplane or a train, or by pigeons - are none of your business, they belong to the lower-level protocol. The post service, in turn, doesn't care about the content of your letter, it just needs to deliver it in time.
A similar thing happens with networking. As you can see on the diagram, HTTP belongs to the highest, Application layer. HTTP services use notions like "URL" or "header", or "content type", but the underlying TCP protocol only cares about delivering the data from one host to another. So, terms like "host" and "port" are from TCP, and terms like "path" are from HTTP.
When you send an HTTP request, for example http://hostname/foo, your host establishes the TCP connection using the host and port information (hostname and 80 is the default port), and then sends the HTTP request over that connection, and that's where path /foo is taken into use.
Now, socket API operates on the transport layer, so it doesn't know anything about paths. You can use socket API to send an HTTP queries, but:
you need to know quite well how HTTP works
it's a lot of manual work
any existing HTTP library uses sockets under the hood
To summarize, you don't really need to deal with sockets for HTTP. Python has modules urllib.request and http.client, but the most popular and easy-to-use Python library for that purpose is requests. With it, sending a request is as simple as:
import requests
response = requests.get('http://localhost/foo')

Related

When do we need socket and when do we need request?

Why do we need socket despite of request library in python?
If we wanna socket to connect to other server so what is request library for?
Request is a higher level API for handling HTTP requests (which uses socket internally). There are dozens of other network protocols not covered by it. Of course, you could handle HTTP by using socket directly, but unless you have an extremely good reason to do so, you'd just be reinventing the wheel.
Requests is a Python HTTP library, whereas sockets are used for sending or receiving data on a computer network. HTTP is an application layer protocol that specifies how request and replies from client and server should be made. In socket programming, you make connection by specifying destination IP/Port and send your data to remote host.

Can I use Firebase functions to receive data which is supposed to be sent to a server on a port?

I have a 4G router with GPS functionality. I'd like to save the GPS info so I can keep track of the router as it's mounted on a vehicle.
The GPS configuration needs a server or domain address and a port. I'm wondering if I can set it up to either send the info to Firebase database directly or make sue of functions to achieve this.
I've tried using python with no luck like below :
import socket
s = socket.socket()
s.connect(('host',port))
... etc ...
But I think opening a socket won't work.
There's no generic socket server in Cloud Functions for Firebase. You could point it to a HTTP-triggered Cloud Function, but that'd only work if the GPS device sends HTTP requests, which seems unlikely given that the protocol starts from a socket (instead of a higher level abstraction layer).

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.

Select outgoing IP from django?

If we're running on a host which can have multiple IP addresses (it's actually EC2 with elastic IPs), is it possible to select from django which outgoing IP address to use?
Even if this is just a random choice it'd be fine.
Edit: Apologies, I was not clear above. The requests are new outgoing calls made from within Python, not a response to a client request - happy for that to go back down whatever pipe it came in on.
I guess that for webapp responses, the server is always going to use one connection socket, so if the request came to IP address X, the response will be sent in the same TCP connection and will originate from the same address X, even though the host also has addresses Y and Z.
On the other hand, if your application creates another TCP connection during its operation, its probably possible to bind that socket on any of host's IP addresses you want. If you're using python's socket module, you can do it by specifying source_address argument in socket.create_connection() call. Unfortunately, not all higher-level libraries may allow this level of control.
I am not sure about the question quite well, but just wanted to drop this page, if it comes to any help python outgoing ip

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