title: Opening a python function in another process
body: <p>I haven't been doing almost any programming before this so I apologize in advance for the quality of my code. </p>
  
  <p>The problem I'm having is that I have a GUI and I need to open a server to receive internet traffic, but if I try to open it in a new process I get an error about a name not being defined (even though it works just fine if I open the server as a regular function and not a new process). The funny thing is that the GUI still opens even though I get the error.</p>
  
  <p>I'll paste the error messages after the code.</p>
  
  <p>Thank you in advance.</p>
  
  <pre><code>from Tkinter import *
  import socket, re
  import netifaces as ni
  from multiprocessing import Process, Manager
  
  class Application(Frame):
  
  
          def __init__(self, master):
            """ Initialize the Frame"""
            Frame.__init__(self,master)
            self.grid()
            self.create_widgets()
  
          def create_widgets(self):
            self.label1 = Label(text = "Target IPv6 address")
            self.label1.grid(row=1, column=0)
  
            self.entry1 = Entry(bd = 5)
            self.entry1.grid(row=1, column = 1)
  
            self.button1 = Button(text = "Start", command = self.clientstart)
            self.button1.grid(row=1, column = 2)
  
            pr1 = Process(target=self.serverstart)
            self.button2 = Button(text = "Start", command = pr1.start())
            self.button2.grid(row=2, column=2)
  
            self.label2 = Label(text = "Choose interface to listen")
            self.label2.grid(row=2, column=0)
  
            self.interfaces = Menubutton(text="------", relief=RAISED)
            self.interfaces.grid(row=2, column=1)
            self.interfaces.menu = Menu(self.interfaces, tearoff=0)
            self.interfaces["menu"] = self.interfaces.menu
            self.menubox()
  
          def menubox(self):
            self.interfaces.menu.add_command(label="------", command = lambda interface="------": self.callback(interface))
            for interface in ni.interfaces():
                  if interface.startswith('eth'):
                    self.interfaces.menu.add_command(label=interface, command = lambda interface=interface: self.callback(interface))
                  else:
                    pass
  
          def callback(self, interface):
            if interface.startswith('eth'):
                  self.interfaces["text"] = interface
            else:
                  self.interfaces["text"] = "------"
            self._netint = interface
  
          def serverstart(self):
            import tcpServer
            tcpServer.start(self._netint)
  
  
          def clientstart(self):
            targetip = self.entry1.get()
            import tcpClient
            tcpClient.startclient(targetip)
  root = Tk()
  
  root.title("IPv6 traffic generator")
  root.geometry("400x600")
  
  app = Application(root)
  
  root.mainloop()
  
  Process Process-1:
  Traceback (most recent call last):
    File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
      self.run()
    File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
      self._target(*self._args, **self._kwargs)
    File "gui.py", line 57, in serverstart
      tcpServer.start(netint)
  NameError: global name 'netint' is not defined
  </code></pre>
  
  <p>EDIT:</p>
  
  <p>I tried the advice in the first reply, but it still gives an error</p>
  
  <pre><code>Process Process-1:
  Traceback (most recent call last):
    File "/usr/lib/python2.7/multiprocessing/process.py", line 258, in _bootstrap
      self.run()
    File "/usr/lib/python2.7/multiprocessing/process.py", line 114, in run
      self._target(*self._args, **self._kwargs)
    File "gui.py", line 55, in serverstart
      tcpServer.start(self._netint)
  AttributeError: Application instance has no attribute '_netint'
  </code></pre>
  
  <p>Here is the code for tcpServer.py</p>
  
  <pre><code>import socket
  import netifaces as ni
  
  def start(self, _netint):
          host = ni.ifaddresses(self._netint)[ni.AF_INET6][0]['addr']
          port = 5000
  
          s = socket.socket(socket.AF_INET6)
          s.bind((host, port))
  
          s.listen(1)
          c, addr = s.accept()
          print "Connection from: " + str(addr)
          while True:
                  data = c.recv(1024)
                  if not data:
                          break
                  print "from connected user: " + str(data)
                  data = str(data).upper()
                  print "sending: " + str(data)
                  c.send(data)
          c.close()
  
  if __name__ == '__main__':
          start()
  </code></pre>
  
  <p>And here for tcpClient.py</p>
  
  <pre><code>import socket
  
  def startclient(targetip):
          host = targetip
          port = 5000
  
          s = socket.socket(socket.AF_INET6)
          s.connect((host,port))
  
          message = raw_input('Send message: ')
          while message != 'q':
                  s.send(message)
                  data = s.recv(1024)
                  print 'Recieved from server: ' + str(data)
                  message = raw_input('Send message: ')
          s.close()
  
  if __name__ == '__main__':
          startclient()
  </code></pre>
  
  <p>Please note that my server and client programs are just for testing for now.</p>