title: Python says global name not defined
body: <p>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.</p>
  
  <pre><code>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 &lt; 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()
  </code></pre>
  
  <p>When I execute this i get the following error saying global name not defined pls help</p>
  
  <pre><code>Traceback (most recent call last):
    File "D:/Sankalp0203/P2Per.py", line 101, in &lt;module&gt;
      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
  </code></pre>