I wrote a short class to help me test sending plain text http requests to a server that I'm working on, but none of the requests I make with it are going through, even requests to domains other than my server. All requests are timing out. The class is just a wrapper around python's socket module. Interestingly, this class used to work just fine a few months ago. I have not touched the code since then.
Source:
class TCPClient:
def __init__(self):
self.connection = None
def sendMessage(self, message):
if self.connection:
bytes_sent = self.connection.send(message)
print "bytes_sent: %d"%bytes_sent
else:
print 'No open connection.'
def getResponse(self, buffer_size=1024):
response = ""
start_time = time.time()
curr_time = time.time()
frombuff = 1
while frombuff and ((curr_time - start_time) < 5):
curr_time = time.time()
frombuff = self.connection.recv(buffer_size)
try:
frombuff = self.connection.recv(buffer_size)
response += frombuff
except Exception as e:
logging.error("Exception thrown while receiving bytes")
logging.error(e)
frombuff = None
return
return response
def openConnection(self, IP, PORT, timeout=2):
if self.connection:
self.connection.close()
self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.connection.connect((IP, PORT))
self.connection.settimeout(timeout)
def closeConnection(self):
if self.connection:
self.connection.close()
self.connection = None
else:
print 'No open connection.'
Here is a script I've been using to test it, with a few modifications. The actual request I've been trying to make is to my live server, to which requests work in browser and with python's requests library. I can't list it here for privacy reasons. The request below times out as well though.
Test Script:
from tcp import TCPClient
msg = "GET /whatever HTTP/1.1\r\n\
Host: google.com\r\n\
Connection: Keep-Alive\r\n\
\r\n"
#expecting a proper 404
client = TCPClient()
client.openConnection(HOST, PORT)
client.sendMessage(msg)
res = client.getResponse()
print res
Any help appreciated. Thanks!
Edit
The timeout is caught in the try/except block in getResponse(). The output is looks like this:
ERROR:root:Exception thrown while receiving bytes
ERROR:root:timed out