Close urllib2 connection

后端 未结 4 798
后悔当初
后悔当初 2021-01-11 23:53

I\'m using urllib2 to load files from ftp- and http-servers.

Some of the servers support only one connection per IP. The problem is, that urllib2 does not close the

4条回答
  •  抹茶落季
    2021-01-12 00:12

    Biggie: I think it's because the connection is not shutdown().

    Note close() releases the resource associated with a connection but does not necessarily close the connection immediately. If you want to close the connection in a timely fashion, call shutdown() before close().

    You could try something like this before f.close():

    import socket
    f.fp._sock.fp._sock.shutdown(socket.SHUT_RDWR)
    

    (And yes.. if that works, it's not Right(tm), but you'll know what the problem is.)

提交回复
热议问题