作業中のサーバーにプレーンテキストの 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