1

Provided having two separate server applications: e.g. ServerAppAlpha and ServerAppBeta, one of them (ServerAppAlpha) shall receive socket connections from clients:

# ServerAppAlpha

import socket

svr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
svr.bind(('127.0.0.1', 9090))
svr.listen(5)

while (True):
    conn, addr = svr.accept() 
    from_clnt = svt.recv(4096)
    # conn == <socket.socket fd=552, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 9090), raddr=('127.0.0.1', 53006)>

    svr2 = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    svr2.connect(('127.0.0.2', 9090))
    # somehow forward both "conn" and "from_clnt" to ServerAppBeta"

and forward both conn and from_clnt to the other one (ServerAppBeta), so that it could directly respond to the client:

# ServerAppBeta

import socket

svr = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
svr.bind(('127.0.0.2', 9090))
svr.listen(5)

while (True):
    conn, addr = svr.accept()
    from_alpha = svr.recv(4096)
    # somehow send something to the client, who sent a request to ServerAppAlpha

Furthermore, the client application is something like this:

# ClientApp

import socket

clnt = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
clnt.connect(('127.0.0.1', 9090))
clnt.send('SampleRequest'.encode()) # Sends to ServerAppAlpha
clnt.recv(4096) # Receives from ServerAppBeta

The primary goal is to have a transparent distributed system, so that the client “feels” dealing with a single server. So the questions are:

  1. How to forward a connection from one server to another?
  2. How can a client send a request to one server and receive the response from another?
Pouyan
  • 165
  • 1
  • 7
  • You cannot forward a *"connection"*. You can only forward the data by reading from one side and writing what what you've read to the other side, and then do this for both directions. – Steffen Ullrich Dec 19 '19 at 11:58
  • So would there be a way to create an object from `` on _ServerAppBeta_ and somehow fill up the args using e.g. `` (the client `conn` which was converted into a string), so that it could directly connect and respond to the client? – Pouyan Dec 19 '19 at 12:14
  • No, this would not be possible. The client would not expect any data from ServerAppBeta on this port, i.e. the connection attempt would fail. – Steffen Ullrich Dec 19 '19 at 12:20
  • If your ServerAppBeta is running on the same system, you could just have your ServerAppAlpha call ServerAppBeta with a socket descriptor, then initialize the socket from that descriptor. Not sure if that will work in windows though. Otherwise, you can just open a thread or process using the multiprocessing or multithreading modules, and pass the socket object as argument. Though I have to admit, I have only tested this with threads. But this won't do you much good if you have multiple systems. – J.Horr Dec 19 '19 at 16:55
  • On a unix (or linux) system, it is possible to pass the socket file descriptor to another process through Unix domain sockets. See this answer: https://stackoverflow.com/a/2358843/1076479. I have never seen it done in a python program, but there is no reason why it couldn't be. It will be tedious to implement and probably require the use of `ctypes` module, but it's possible. – Gil Hamilton Dec 19 '19 at 21:32

0 Answers0