0

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
Community
  • 1
  • 1
bgenchel
  • 3,739
  • 4
  • 19
  • 28
  • Sounds like a problem on the server end, not the client. Has the server changed recently? Is the server running? – John Gordon Feb 02 '16 at 18:20
  • no, it hasn't. It is definitely running; browser requests work fine, as well as requests made with python's requests lib. Even if it were down, you'd think I would get the proper 404 response from my google request, but that times out as well. – bgenchel Feb 02 '16 at 18:22
  • Fix your indentation in the question please. – Torxed Feb 02 '16 at 18:29
  • Which row reports the error? What does the error say? – Torxed Feb 02 '16 at 18:31
  • you'll have to be more specific. It looks fine to me, perhaps this is a matter of taste? The error is caught in the try/except block. I'll put the output in the question. – bgenchel Feb 02 '16 at 18:31

1 Answers1

0

Well, I feel like an idiot. The problem with the script is just the way I wrote the getResponse Method. For some reason, I wasn't appending the first bytes I get to the response string, and I just delete whatever response is there if it times out.

If I'm not mistaken (??), it should always time out, when it's done receiving and there are no more bytes to get from the server. Once I reorganized that block, I started to receive correct responses.

Something still seems fishy to me about this solution though... is there supposed to be an ending sign outside of a timeout? Is the ending a timeout just because of the way I wrote it?

original

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

modified

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)
            break
            # frombuff = None

    return response
bgenchel
  • 3,739
  • 4
  • 19
  • 28