1

I'm trying to collect data which is being parsed via a socket. Here is my code:

import pickle
import SocketServer

class SocketReciever(SocketServer.BaseRequestHandler):

    def handle(self):
        sint = self.request.makefile('rb')
        objectt = pickle.load(sint)
        #print(objectt)
        ParentClassCall(objectt)

if __name__ == "__main__":
    HOST, PORT = "localhost", 60

    # Create the server, binding to localhost on port 9999
    server = SocketServer.TCPServer((HOST, PORT), SocketReciever)
    # Activate the server; this will keep running until you
    # interrupt the program with Ctrl-C
    server.serve_forever()

data=[]
def ParentClassCall(currentdata):
    data.append(currentdata)

My question is how would I call the ParentClassCall function from within the SocketReciever class?

I know this method is plagued with security problems but it will be run on a computer without internet access.

Uzer
  • 3,054
  • 1
  • 17
  • 23
  • 2
    I'm not sure what this has to do with inheritance. You call `ParentClassCall` as you would any function, as `ParentClassCall(whateverdata)`. And it is called in that code (`ParentClassCall(objectt)`). – David Robinson Feb 07 '13 at 17:46
  • Hi David, when ParentClassCall is called within the socket reciever. I get this message: NameError: global name 'ParentClassCall' is not defined. Which is expected as ParentClassCall is above the SocketReciever class in the hierarchy. I wondering how it could be done. – Uzer Feb 07 '13 at 17:55
  • That doesn't have anything to do with a hierarchy or inheritance. In this example, `ParentClassCall` isn't a class or even a method of a class, it's just a regular function. – David Robinson Feb 07 '13 at 18:08
  • Ok then, as I said, I am a noob. What I was thinking before was all the code belongs to a file which can be imported as an object hence its sort of an instance of a class, with the ParentClassCall being a function of that 'class'. Either way this does not tell me how I could achieve what I asked. – Uzer Feb 07 '13 at 18:14
  • I've changed the name of the question as it not as you say relevant. – Uzer Feb 07 '13 at 18:19
  • I other words how would I bind the function 'ParentClassCall' to the handle event and parse the data. – Uzer Feb 07 '13 at 18:23

2 Answers2

2

Python never gets to defining ParentClassCall() since it stops at the line server.serve_forever(). Define the function before the main stanza.

Ignacio Vazquez-Abrams
  • 776,304
  • 153
  • 1,341
  • 1,358
2

Here's a simplified version of your example, to demonstrate the problem:

class Foo(object):

  def __init__(self):
    pass

  def do_something(self):
    not_yet_defined_function()

if __name__ == "__main__":
  foo = Foo()
  foo.do_something()

def not_yet_defined_function():
  print "It worked!"

The result is the same:

Traceback (most recent call last):
  File "tmp.py", line 11, in <module>
    foo.do_something()
  File "tmp.py", line 7, in do_something
    not_yet_defined_function()

The problem is that you are trying to access the function before it's defined. The python interpreter reads through the file sequentially, executing commands in order. The class and def keywords are just commands that create (class and function) objects. So, you need to make sure you define all your objects before you start using them.

By changing the example to define the function first:

class Foo(object):

  def __init__(self):
    pass

  def do_something(self):
    not_yet_defined_function()

def not_yet_defined_function():
  print "It worked!"

if __name__ == "__main__":
  foo = Foo()
  foo.do_something()

Then you get the result you want:

lap:~$ python tmp2.py
It worked!
John Hazen
  • 1,296
  • 1
  • 8
  • 19