Python说没有定义全局名称

时间:2016-10-29 17:43:24

标签: python multithreading p2p

这是基于我正在处理的套接字的p2p代码。 每当我尝试执行此操作时,我都会得到未定义的全局名称错误。

import socket
import os.path
import sys
import urlparse
import threading

class Node:

        def startlisten(sock):
                sock.listen(6)
                while True:
                    c,addr = sock.accept()
                    print "Client connected all set to go : "+str(addr)
                    thread1 = threading(target=retrfile,args=(c,))
                    thread1.start()

        def __init__(self):
                self.port=raw_input("Enter the port: ")
                self.shareddir=raw_input("Enter the name of the folder you want to share: ")
                self.sysname=raw_input("Enter your desired nick name: ")
                self.known=set()
                self.s=socket.socket()
                host = socket.gethostname()
                port = int(self.port)
                print host,port

                self.s.bind((host,port))
                t=threading.Thread(target=startlisten,args =(self.s,))
                t.start()



        def retrfile(sock):
            code,filename = sock.recv(1024)
            if code == 0:
                if os.pathisfile(os.path.join("D:\\Sankalp0203\\",filename)):
                    sock.send("OK",str(os.path.getsize(filename)))
                    userResponse = sock.recv(1024)
                    if userResponse == 'Y':
                        with open(filename,'rb') as f:
                            bytestoread = f.read(1024)
                            sock.send(1024)
                            while bytestoread != "":
                                bytestoread = f.read(1024)
                                sock.send(byestoread)




        def _hello(self,other):
                self.known.add(other)

        def _broadcast(self,query,history):
                for other in self.known.copy():
                        if other in history:
                                continue
                        try:
                                name = urlparse(other)[1]
                                parts=name.split(":")
                                port = int(parts[-1])
                                host = "http://"+parts[0]
                                self.s.connect((host,port))
                                self.s.send(0,query)
                                code , size = s.recv(1024)
                                if code == "OK":
                                    inp = raw_input("File Exists and filesize :  "+str(size)+" download? y/n: ")
                                    if inp == Y:
                                        self.s.send(Y)
                                        write(self.s,size)
                                    return code , size,
                        except:
                                self.known.remove(other)
                return FAIL,EMPTY

        def write(sock1,size):
            f = open('new'+filename,'wb')
            data = sock1.recv(1024)
            totalrecv = len(data)
            f.write(data)
            while(totalrecv < size):
                data = sock1.recv(1024)
                f.write(data)
                totalrecv+=len(data)




def Main():
        n=Node()

        num=3
        while(num):
                input = (int)(raw_input("Enter 1 for fetch and 2 to sit idle and 3 to introduce to new peer"))
                if(input == 1):
                        filename = raw_input("Enter the name of the file")
                        n.query(filename)
                if(input == 3):
                        frnd =raw_input("Enter the url of the friend socket")
                        n._hello(frnd)
if __name__=='__main__':
        Main()

当我执行此操作时,我收到以下错误,说明未定义全局名称请帮助

Traceback (most recent call last):
  File "D:/Sankalp0203/P2Per.py", line 101, in <module>
    Main()
  File "D:/Sankalp0203/P2Per.py", line 89, in Main
    n=Node()
  File "D:/Sankalp0203/P2Per.py", line 28, in __init__
    t=threading.Thread(target=startlisten,args =(self.s,))
NameError: global name 'startlisten' is not defined

1 个答案:

答案 0 :(得分:0)

它说未定义startlisten,因为未定义全局名称startlisten。没有名为startlisten的全局函数。您创建的方法是在Node类下。你错过了自我。正确的方法是:

t=threading.Thread(target=self.startlisten,args =(self.s,))