I build an app compatible between Python 2 and 3. In order to provide compatibility between those two Python versions, I use six library.
My code uses sockets. Under Python 3 it is possible to create them using with
statement, but under Python 2 it claims on missing __exit__
attribute.
Is there any function of six providing disposable socket? If not, what solution would you consider as the most clear in this case?
AFAIK (by scanning through its docs)
six
doesn't do much in this case. In Python 2, you could wrapsocket
up in a context manager with thecontextmanager
decorator and supply that:In both cases you use the
with
statement accordingly: