1

我想创建一个 python 脚本来对我的服务器进行压力测试。所以我创建了这个基本的 UDP 泛洪器,我认为它可以正常工作。

我的问题是我将如何为此添加多线程?我阅读了有关 Python 线程的手册,但不明白如何在我的脚本中实际实现它。

import socket
import random

print "Target:",
ipaddr = raw_input()

sent = 1
bytes = random._urandom(10000)
port = 1

while sent > 0:
    print "Test Started On", ipaddr, "|", sent, "Packets Sent. Press Ctrl+C To Stop."
    sent += 1
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.sendto(bytes,(ipaddr,port))
port = random.randint(1, 65500)

raw_input()
4

1 回答 1

1

If you extract the business part of your application into a function like:

def do_the_flooding():
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    s.sendto(bytes,(ipaddr,port))

You can then call it in a thread:

import threading
t = threading.Thread(target=do_the_flooding)
t.start()
于 2012-07-09T05:12:31.747 回答