I am trying to write a simple text/HTTP server which in python to simulate a variety of bad network conditions. In the first place, I want a server which will appear to close the connection without sending the browser any data in order to reliably create the "net::ERR_EMPTY_RESPONSE" browser error. Most tutorials about socket programming deal with how to create good servers, not how to create bad servers so I'm sure you can appreciate my problem here.
Here's what I have so far:
import socket, sys, time
port = 8080
print "Bad server is listening on " + str(port)
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
serversocket.bind(('localhost', port))
serversocket.listen(5)
#simulate successful transport after the 5th attempt
counter = 0
while True:
#accept connections from outside
(clientsocket, address) = serversocket.accept()
time.sleep(1);
if counter > 5:
serversocket.write('<span class="position: relative; left: 20px;>hello</span>');
serversocket.close()
counter += 1
The counter is because I thought it might be nice if the server replies after the 5th connection attempt in order to simulate a very unreliable network connection.
However, despite being a bad server, the bad server should not itself crash. When I run that code I get:
Bad server is listening on 8080
Traceback (most recent call last):
File "bad_server.py", line 16, in <module>
(clientsocket, address) = serversocket.accept()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 195, in accept
sock, addr = self._sock.accept()
File "/System/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/socket.py", line 165, in _dummy
raise error(EBADF, 'Bad file descriptor')
socket.error: [Errno 9] Bad file descriptor
This is not desirable. The bad server should carry on listening for connections.
My next task after getting this working is to try to make a bad server abruptly terminates its reply, resulting in the net::ERR_ABORTED in a browser. I have no idea how I'll go about that though. Any help would be appreciated.