type(socket) != SocketType

Chris Liechti cliechti at gmx.net
Sat Jun 15 23:19:29 CEST 2002


Irmen de Jong <irmen at NOSPAMREMOVETHISxs4all.nl> wrote in 
news:3D0BAD4D.5050105 at NOSPAMREMOVETHISxs4all.nl:

> Hello
> 
> I have to detect if a certain socket object is a regular socket,
> or a M2Crypto SSL socket. I do this by checking type(sock),
> and then checking if this is a regular socket type. Why this way?
> because the systems that run my code do not necessarily have
> M2Crypto installed, and thus I cannot compare the socket to
> any class/type from M2Crypto, nor just go ahead and catch SSL
> exceptions (simply because they are not defined!).
> 
> There is a problem, however.
> With Python 2.2.1 on Windows, type(socket) != SocketType:
> 
> >>> from socket import *
> >>> s=socket(AF_INET,SOCK_STREAM)
> >>> type(s)
> <type 'instance'>
> >>> SocketType
> <type '_socket.socket'>
> >>> type(s) is SocketTye
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
> NameError: name 'SocketTye' is not defined
> >>> type(s) is SocketType
> 0
> >>> type(s)== SocketType
> 0
> >>>
> 
> 
> I find this most unfortunate, because the docs on the socket module
> explicitly state that type(socket)=SocketType.

what happens to people with timoutsocket? that module repaces the 
socket.socket class with a custom one and your check will fail....

> BTW on Linux, all is fine.
> 
> Any clues how to fix things on windows?

no, sorry
maybe there is a cleverer solution that typechecking. the SSL socket has 
other/additional attributes etc right?. check for them with has_attr(). or 
use the __name__ attribute of the class. however, typechecking is probably 
not the right way to go.

chris

> Thanks a lot,
> Irmen
> 
> 



-- 
Chris <cliechti at gmx.net>




More information about the Python-list mailing list