0

We can specify the Server IP/Port while connecting a Client using:

c = rpyc.connect("127.0.0.1",port=18812)

However, I'd like to know if there is a way in which we can specify which port the Client would use OR if we can identify which port the Client will use each time it tries to connect to the server.

This can be identified if logging is enabled on the Server:

INFO:MASTERSERVER/18812:server started on [127.0.0.1]:18812
INFO:MASTERSERVER/18812:accepted ('127.0.0.1', 62372) with fd 664
INFO:MASTERSERVER/18812:welcome ('127.0.0.1', 62372)
INFO:MASTERSERVER/18812:goodbye ('127.0.0.1', 62372)

However, I'd like to know if there's a way to get this info from & on the Client.

ParvBanks
  • 1,316
  • 1
  • 9
  • 15
  • Why do you need to do this? Specifying a particular local port will prevent multiple clients from the same machine connecting to the same server. – Barmar Jul 31 '19 at 05:53
  • see this : https://rpyc.readthedocs.io/en/latest/api/utils_factory.html#rpyc.utils.factory.ssh_connect – LinPy Jul 31 '19 at 05:56
  • @Barmar, I'm just curious.. Let's say if we want only specific clients to be able to connect to the server. Or if we wish to print/ log the client port to the client window/ file? – ParvBanks Jul 31 '19 at 06:11
  • @LinPy, thanks for this.. ssh tunnels using Plumbum does seem to be one of the options.. will look into it. – ParvBanks Jul 31 '19 at 06:13

1 Answers1

0

Here is a way to achieve the requirement:

import rpyc
conn = rpyc.connect("localhost", 18812)
socket = conn._channel.stream.sock
print(socket.getsockname()) # what you are looking for
print(socket.getpeername()) # extra information

This will print out:

('127.0.0.1', 50086)
('127.0.0.1', 18812)