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:
- How to forward a connection from one server to another?
- How can a client send a request to one server and receive the response from another?