我的python套接字脚本有什么问题?

时间:2016-02-02 18:17:53

标签: python sockets tcp python-sockets

我写了一个简短的类来帮助我测试将纯文本http请求发送到我正在处理的服务器上,但是我用它做的请求都没有通过,甚至是对我服务器以外的域的请求。所有请求都超时。该类只是python的套接字模块的包装器。有趣的是,几个月前这个班级曾经工作得很好。从那以后我没有触及过代码。

来源:

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.'

这是我用来测试它的一个脚本,只做了一些修改。我一直想要的实际请求是我的实时服务器,请求在浏览器和python的请求库中工作。出于隐私原因,我无法在此列出。下面的请求也会超时。

测试脚本:

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

任何帮助表示赞赏。谢谢!

修改

超时在getResponse()的try / except块中捕获。输出如下所示:

ERROR:root:Exception thrown while receiving bytes
ERROR:root:timed out

1 个答案:

答案 0 :(得分:0)

嗯,我觉得自己像个白痴。脚本的问题就在于我编写getResponse方法的方式。出于某种原因,我没有将我得到的第一个字节附加到响应字符串中,如果超时,我只删除那里的任何响应。

如果我没有弄错(??),它应该总是超时,当它完成接收并且没有更多的字节从服务器获取。一旦我重组了那个区块,我就开始收到正确的回复。

对于这个解决方案,我仍然看起来有点可疑......但是在超时之外应该有一个结束符号吗?结尾是因为我写它的方式而暂停了吗?

原始

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 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