[WinError 10048] Only one usage of each socket address ...

I notice that despite using requests.Session() - I still seem to be creating new connections/sockets which eventually exhaust (TIME_WAIT) and I get the following error:

> [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted',))

```
s = requests.Session()
data = zip(url_routes, cycle(s))
calc_routes = pool.map(processRequest, data)

```

I posted a bit more [here](http://stackoverflow.com/questions/35793908/python-multiprocessing-associate-a-process-with-a-session), however not sure how to address this


What does `processRequest` do?


FWIW, I should mention that it's _extremely_ unwise to use a Session from multiple processes at the same time: it's not clear to me at all how well connection pooling will behave in that situation, and any number of things could start misbehaving. It's vastly preferable to have one Session _per process_ if possible.


I have a similar question

after executing this:

```
with requests.Session() as s:
        r= s.get('http://192.168.1.4:8080/health')
        r.close()
```

netstat shows 
`tcp        0      0 192.168.1.2:44984       192.168.1.4:8080        TIME_WAIT`

looks like the socket is not shutdowned. of course OS will shutdown it in a minute by default, but how to accomplish it immediately?


The side of the connection that _closes_ a socket is always moved into `TIME_WAIT`. This cannot be avoided: due to the nature of TCP, it is possible that the final ACK will be lost and so the socket needs to be held around for a while to give confidence that it has not been. This is what `TIME_WAIT` is for. It cannot be avoided if you're closing the socket yourself.


this doesn't have that side effect.
after run this code socket is destroyed.
to avoid TIME_WAIT state it's necessarily run shutdown() before close()  
[https://docs.python.org/3/library/socket.html?highlight=socket#socket.socket.close]

```
import socket

HOST = 'localhost'    # The remote host
PORT = 8080              # The same port as used by the server
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
    s.connect((HOST, PORT))
    s.send(b'GET /health\n\n')
    data = s.recv(20)
```


The reason your code doesn't have that side effect is because the _server_ is closing the connection first. That's because your code is performing a HTTP/0.9 style GET request, which requires the server to close the connection to signal the end of the response. That means that the _server_ is almost certainly getting a socket in TIME_WAIT instead, which is _much_ worse than you getting a socket in TIME_WAIT. 

To avoid getting sockets in TIME_WAIT, the best thing to do is to use a single Session object at as high a scope as you can and leave it open for the lifetime of your program. Requests will do its best to re-use the sockets as much as possible, which should prevent them lapsing into TIME_WAIT. But you cannot avoid TIME_WAIT if you want to be closing the sockets yourself: it is a critical part of the TCP state machine that exists for a very good reason.

Shutdown does not prevent TIME_WAIT. I don't really know why you think it does.


Related question:  If I want to reuse the TCP connection _only_, is using a Session and clearing the Session's cookies and not using session.auth enough?  Or is there more I should clear?  (I'm simulating very large numbers of users bombarding a service.)

@tlc Cookies should be enough.