Python说未定义全局名称 [英] Python says global name not defined

查看:198
本文介绍了Python说未定义全局名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

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

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

这篇关于Python说未定义全局名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆