Python says global name not defined

☆樱花仙子☆ 提交于 2019-12-13 23:29:38

问题


This is the code for p2p based on sockets that I am working on. Whenever I try to execute this I get global name not defined error.

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()

When I execute this i get the following error saying global name not defined pls help

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:


It's saying that startlisten is not defined because global name startlisten is not defined. There is no global function called startlisten. The method you created is under the Node class. You missed the self. The right way to do it would be:

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


来源:https://stackoverflow.com/questions/40321850/python-says-global-name-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!