classification
Title: nice to have a way to tell if a socket is bound
Type: enhancement Stage: test needed
Components: Versions: Python 3.2
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: janssen Nosy List: Ramchandra Apte, janssen, loewis, martin.panter, vila
Priority: low Keywords:

Created on 2007-08-30 17:53 by janssen, last changed 2014-04-17 05:42 by martin.panter.

Messages (7)
msg55494 - (view) Author: Bill Janssen (janssen) * (Python committer) Date: 2007-08-30 17:53
It would be useful to have a way to determine whether a socket is or is 
not already bound to a local port (i.e., has had "bind" or "connect" 
called on it).  It's tempting to call socket.socket.getsockname(), but 
the behavior of this method is essentially undefined (i.e., it's 
whatever the underlying platform feels like doing, and there seem to be 
no constraints on that -- Unix systems typically return what seems to be 
someone's idea of a null address, while Windows systems currently raise 
an exception.).  So an extension API is needed to probe this state.

Suggest adding a method "is_bound" which returns a boolean.
msg55496 - (view) Author: Bill Janssen (janssen) * (Python committer) Date: 2007-08-30 17:55
What happened to my issue metadata?
msg55500 - (view) Author: Martin v. Löwis (loewis) * (Python committer) Date: 2007-08-30 18:41
Given that the underlying platform has no support for that, it will be
difficult to implement correctly across all systems.
msg55515 - (view) Author: Bill Janssen (janssen) * (Python committer) Date: 2007-08-30 21:41
Indeed.  Calls for some design chops.  Again, it's a question of what
the socket.socket class really is.

Bill

On 8/30/07, Martin v. Löwis <report@bugs.python.org> wrote:
>
> Martin v. Löwis added the comment:
>
> Given that the underlying platform has no support for that, it will be
> difficult to implement correctly across all systems.
>
> ----------
> nosy: +loewis
>
> __________________________________
> Tracker <report@bugs.python.org>
> <http://bugs.python.org/issue1062>
> __________________________________
>
msg149025 - (view) Author: Ramchandra Apte (Ramchandra Apte) * Date: 2011-12-08 12:49
perhaps you can subclass socket.socket and make a function wrapper around bind and connect that sets a variable if called
like:
class sock(socket.socket):
    def bind(self,*args):
        self.is_bound = True
msg149026 - (view) Author: Ramchandra Apte (Ramchandra Apte) * Date: 2011-12-08 12:51
oops should be
class sock(socket.socket):
    _bind = socket.socket.bind
    def bind(self,*args):
        self.is_bound = True
        self._bind(self,*args)
msg216657 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2014-04-17 05:42
The suggested subclass might have to call the default bind(("", 0)) before running certain other operations, including connect(), send[to](), recv[from](), since these operations are meant to automatically bind if necessary.
History
Date User Action Args
2014-04-17 05:42:13martin.pantersetnosy: + martin.panter
messages: + msg216657
2011-12-08 12:51:20Ramchandra Aptesetmessages: + msg149026
2011-12-08 12:49:13Ramchandra Aptesetnosy: + Ramchandra Apte
messages: + msg149025
2010-08-07 20:39:56terry.reedysetstage: test needed
versions: + Python 3.2, - Python 2.6, Python 3.0
2008-01-05 15:07:56vilasetnosy: + vila
2007-08-30 21:41:18janssensetmessages: + msg55515
2007-08-30 18:41:58loewissetnosy: + loewis
messages: + msg55500
2007-08-30 17:55:11janssensetversions: + Python 2.6, Python 3.0
title: nice to have a way to tell if a socket is bound
messages: + msg55496
priority: low
assignee: janssen
type: enhancement
2007-08-30 17:53:19janssencreate