I have a simple server written that extends the SimpleHTTPRequestHandler
If I start and stop it without making any requests to the server, I can start back up on the same port with no problem.
When started, a netstat looks like this:
sam@hersheezy:server$ sudo netstat -na --program | grep 8001 tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 23392/python
After a request is made, netstat looks like this (even after the request has completed):
sam@hersheezy:server$ sudo netstat -na --program | grep 8001 tcp 0 0 0.0.0.0:8001 0.0.0.0:* LISTEN 23392/python tcp 0 0 127.0.0.1:8001 127.0.0.1:48659 TIME_WAIT -
Then, I kill the server using C-c and netstat looks like this (at this point I cannot restart the server because port is already in use):
sudo netstat -na --program | grep 8001 tcp 0 0 127.0.0.1:8001 127.0.0.1:48674 TIME_WAIT -
I am obviously not closing something correctly. My code that sends the reply looks like the following:
"""
reply is an object that can be json encoded that is written with a response code 200
"""
def send_provider_reply(self, replyobj):
try:
str_reply = json.dumps(replyobj)
self.send_response(200)
self.send_header('Content-type', 'application/json')
self.end_headers()
#do we need to send a newline??
self.wfile.write(str_reply)
except:
traceback.print_exc()
self.send_err(500, 'failed after provider creation')