【问题标题】:python multi-threading蟒蛇多线程
【发布时间】:2012-07-17 07:44:25
【问题描述】:

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

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

【问题讨论】:

    标签: python multithreading python-multithreading


    【解决方案1】:

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

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

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

    【讨论】:

    • 我阅读了你的全局函数讨论链接,但我不明白如何在我的代码中使用它
    • 我认为讨论的本质是:不要使用它!使用一个函数从命令行收集所有参数并让它返回一个 dict 或任何你喜欢的,在你初始化线程时使用它作为参数(添加一个 init)然后访问它们作为对象属性(self.header 或 self.parameters['header'] 取决于您的操作方式)
    【解决方案2】:

    您需要为 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

    了解更多信息。

    希望这会有所帮助:)

    【讨论】:

    • 这段代码由于某种奇怪的原因无法工作,它只是因以下错误而崩溃:导入:无法抓取鼠标': Resource temporarily unavailable @ error/xwindow.c/XSelectWindow/9052. from: can't read /var/mail/time import: unable to grab mouse ':资源暂时不可用@error/xwindow.c/XSelectWindow/9052。 ./fuzzy.py:第 6 行:意外标记附近的语法错误 (' ./fuzzy.py: line 6: class ConnectionThread (threading.Thread):' 来自:无法读取 /var/mail/time ./fuzzy.py:第 6 行:附近有语法错误意外令牌(' ./fuzzy.py: line 6: class ConnectionThread (threading.Thread):'
    猜你喜欢
    • 2011-01-29
    • 2010-10-27
    • 2013-01-05
    • 1970-01-01
    • 2010-10-21
    • 2016-05-28
    • 2014-08-01
    • 2021-06-28
    • 1970-01-01
    相关资源
    最近更新 更多