How can I get the file descriptor for the port on which the client has been bound.
I want this to share the port in between different processes.
I am able to get the file descriptor for the tcp server, but I am not able to get the
file descriptor for the client. For example:
p=reactor.listenTCP(8005,Myfactory())
where p is the port object, here I can get the file-descriptor just by using p.fileno()
But in case of client
p=reactor.connectTCP('127.0.0','8080',MyFactory())
here if do p.fileno()
I am getting the error as follows
AttributeError: 'int' object has no attribute 'fileno'
That I know it is the integer, but my question is: is there any other way to get the file descriptor for the client?
Well… what you want to do is a little bit hacky.
To start with, the result of connectTCP() is useless for you, because at this point, no connection has been made yet.
You can start thinking about extracting the file descriptor after the connection has been established, so the connectionMade() method of the protocol instance has been called. The information about file descriptor is hold by the transport, which implement ITransport interface. Unfortunately this interface does not expose information about the file descriptor, as its more general.
In case of a simple TCP connection, your transport will be the instance of twisted.internet.protocol.FileWrapper, which has a file attribute. Summing all of this, your code could look somewhat like: