Ho una domanda. Mi piacerebbe inviare flussi continui di byte a qualche host per un certo periodo di tempo (diciamo 1 minuto) usando python.python: come inviare pacchetti in multi-thread e poi il thread si uccide da solo
Ecco il mio codice finora:
#! /usr/bin/env python
import socket
import thread
import time
IP = "192.168.0.2"
PADDING = "a" * 1000 #assume the MTU is slighly above 1000
DATA = PADDING + "this is sentence number = "
PORT = 14444
killed = False
test_time = 60 #60 seconds of testing
def send_data():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((IP, PORT))
count = 1
starttime = time.clock()
while elapsed < test_time:
sent = s.send(DATA + str(count) + "\n")
if sent == 0: break # assume that if nothing is sent -> connection died
count = count+1
elapsed = time.clock() - starttime
if killed:
break
s.close()
print str(count) + " has been sent"
print "to quit type quit"
thread.start_new_thread(send_data,())
while True:
var = raw_input("Enter something: ")
if var == "quit":
killed = True
Pochi domanda, c'è un modo migliore per lasciare un dado filo dopo 60 secondi diversi polling del time.clock ogni volta? Quando eseguo questo programma, invia correttamente i byte ma quando ho digitato quit l'altro thread non morirà, anche se imposto la variabile killed = True. Mi chiedo perché è così? l'ambito di var Killed dovrebbe raggiungere l'altro thread giusto?
Grazie
Un host esiste effettivamente a quell'indirizzo? Stai usando netcat per catturare l'output o qualche altro programma? – johnny
penso che l'ambito di "ucciso" sia ok. – Jiri