1

スレッドでコードを使用するときに問題があります。問題は、スレッド部分の外側で定義した変数がスレッド部分の内側で定義されていないことです。ここに私のコードがあります:

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
4

2 に答える 2

1

global変数がグローバル変数であることを示すために使用する必要があります。たとえば、ここを参照してください

たとえば、次をrun( の上if)に追加します。

global host, header, ... # All the other variables you use
于 2012-07-17T07:47:03.487 に答える
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

追加情報については。

お役に立てれば :)

于 2012-07-17T08:43:29.510 に答える