我正在創建一個小腳本,以使用Python 3套接字庫將呼叫記錄發送到服務器。Python socket.sendall發送顯然是任意長度
每條記錄的長度都是109個字節。
當我通過套接字發送器循環並觀察接收端以及wireshark時,發現有些數據包太大而無法正確。 Wireshark顯示具有正確數據的數據包長度爲170。有一些數據包長度超過1500。
有時,接收端會拒絕2個長度爲59和50的數據包(合計爲1個完整數據包)。
有沒有辦法讓這個工作更可靠?
以下是我正在使用的方法。
def connect_to_collector(iterator_obj, host, port, rate, count):
if count == None:
count = len(iterator_obj.mylist)
delay = 3600/int(rate)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((host, int(port)))
print ('\n[+] Connected to {} on port {}'.format(host, port))
start = timer()
i = 0
for record in iterator_obj:
if i < count:
try:
if len(record) is not length:
print ('the length was ', length)
s.sendall(record)
time.sleep(delay)
i += 1
except:
e = sys.exc_info()
print ('SEND ERROR OCCURRED ', e)
print ('\n[+] Sent {} to {} on port {} in {:.4f} seconds'.format(i, host, port, timer() - start))
sys.exit()
if verbose:
print ('Sending : |{:' '<150}| record number |{}|'.format(record, i))
if i >= count:
break
# Clean up
print ('\n[+] Sent {} to {} on port {} in {:.4f} seconds'.format(count, host, port, timer() - start))
s.close()
編輯:有一個在
for record in iterator_obj:
if i < count:
try:
if len(record) is not length:
print ('the length was ', length)
s.sendall(record)
錯誤的迭代器內的同時,會偶然sendall()被調用了同一條記錄多次。
我刪除了那個循環。
這是有道理的。另外,我只是想指出後代的緣故,我的代碼中有一個錯誤導致它發送重複。將在更正中編輯。 – Balrizangor 2014-11-07 06:21:45