python多线程

时间:2012-07-17 07:44:25

标签: python multithreading python-multithreading

我在使用带有线程的代码时遇到问题,问题显然是我在线程部分之外定义的变量没有在线程部分内定义,这是我的代码:

import sys
import socket
from time import sleep
import threading

ip = raw_input ("please insert host ip: ")
port = input ("please insert port to fuzz: ")
header = raw_input ("please enter the header you want to fuzz, put & in the place you want to fuzz: ")
packet = raw_input ("what string would you like to fuzz the header with? : ")
multi = input ("in what jumps would you liike to multiply the string ? : ")
process = input ("please insert number of threads: ")
host = ip, port
char = packet * multi
a = 1

class ConnectionThread ( threading.Thread ):
    def run ( self ):
        while a > 0:
            try:
                s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
                s.connect((host))
                header = header.replace("&", packet)
                s.send(header)
                s.settimeout(7)
                data = s.recv(4)
                if data > 0:
                    print "got awnser"
                else:
                    print "no awnser"   
                sleep(0.1) 
                print "Fuzzing With:", header
                header = header.replace (packet, "&")
                packet = char + packet 
                s.close()
            except Exception as e:
                print e   
                s.close()
                sys.exit(0)
for x in xrange ( process ):
   ConnectionThread().start()

我将此作为回报

local variable 'header' referenced before assignment

2 个答案:

答案 0 :(得分:1)

您需要使用global来表示变量是全局变量。例如,请参阅here

例如,将以下内容添加到runif上方):

global host, header, ... # All the other variables you use

答案 1 :(得分:0)

您需要为ConnectionThread类提供要传递给它的属性。在该类的顶部只放入一个 init 方法,这将定义您要传递给run方法的变量:

import sys
import socket
from time import sleep
import threading

class ConnectionThread ( threading.Thread ):
    def __init__(self):
        self.ip = raw_input ("please insert host ip: ")
        self.port = input ("please insert port to fuzz: ")
        self.header = raw_input ("please enter the header you want to fuzz, put & in the place you want to fuzz: ")
        self.packet = raw_input ("what string would you like to fuzz the header with? : ")
        self.multi = input ("in what jumps would you liike to multiply the string ? : ")
        self.process = input ("please insert number of threads: ")
        self.host = self.ip, self.port
        self.char = self.packet * self.multi
        self.a = 1

    def run ( self ):
        while self.a > 0:
            try:
                s = socket.socket( socket.AF_INET, socket.SOCK_STREAM)
                s.connect((self.host))
                self.header = self.header.replace("&", self.packet)
                s.send(self.header)
                s.settimeout(7)
                data = s.recv(4)
                if data > 0:
                    print "got awnser"
                else:
                    print "no awnser"   
                sleep(0.1) 
                print "Fuzzing With:", header
                self.header = self.header.replace (self.packet, "&")
                self.packet = self.char + self.packet 
                s.close()
            except Exception as e:
                print e   
                s.close()
                sys.exit(0)

for x in xrange ( ConnectionThread.process ):
   ConnectionThread().start()

了解班级的工作方式:

http://docs.python.org/tutorial/classes.html

了解更多信息。

希望这会有所帮助:)