2012-07-17 124 views
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 

回答

0

你需要給你的ConnectionThread類,要傳遞給它的屬性。在剛剛放在一個初始化方法的類的頂部,這將定義要傳遞給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

瞭解更多信息。

希望這有助於:)

+0

此代碼爲一些奇怪的原因不會工作,它只是與此錯誤崩潰:導入:無法抓住鼠標':資源暫時不可用@錯誤/ xwindow。 C/XSelectWindow/9052。 from:無法讀取/ var/mail/time 導入:無法抓取鼠標':資源暫時不可用@ error/xwindow.c/XSelectWindow/9052。 (' ./fuzzy.py:line 6:'class ConnectionThread(threading.Thread):' from:can not read/var/mail /時間 ./fuzzy.py:第6行:意外標記附近的語法錯誤'(' ./fuzzy.py:第6行:'ConnectionThread(threading.Thread):' – Ba7a7chy 2012-07-17 09:15:10

1

您需要使用global來表示變量是全球性的。見,例如,here

例如,添加以下run(在if以上):

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

我看了你的鏈接,全局函數的討論,但我不知道如何使用它在我的代碼 – Ba7a7chy 2012-07-17 09:16:01

+1

我想討論的實質是:不使用它!使用一個函數從命令行收集所有參數,並讓它返回一個字典或任何你喜歡的,在初始化線程時使用它作爲參數(添加一個__init__),然後作爲對象屬性(self.header)訪問它們或自我。參數['標題']取決於你的方式) – 2012-07-17 09:41:26