-1

I was looking for FTP server implemented in Python and came across this https://gist.github.com/scturtle/1035886 . I tried to understand it but being a web developer and fairly new Python I found it confusing.

Here's what I don't understand :

  1. From where did this attribute appeared , Its not defined anywhere
    Line no : 22, 23

    def run(self):
        self.conn.send('220 Welcome!\r\n')
    
  2. Where is this function / code defined ?
    Line no : 223, 224

    ftp.daemon=True
    ftp.start()
    

I know basic OOP in Python any reference material to understand the code and become more power Python programmer will be appreciable . Thanks!

ashwinjv
  • 2,787
  • 1
  • 23
  • 32
Shubham
  • 80
  • 2
  • 13

2 Answers2

0

In an assignment, python will create the attribute (variable) if not present. So in init it actually does self.conn=conn to create the attribute. __init__ is pretty much the constructor that is called when an instance of the class is made. Same for ftp.daemon. Now, ftp.start() is inherited from threading.Thread.

spalac24
  • 1,076
  • 1
  • 7
  • 16
0

(1) The variable is defined in __init__

def __init__(self,(conn,addr)):
    self.conn=conn
    ...

(2) FTPserverThread inherits from threading.Thread. The attribute and method are defined in the parent class.

When puzzling through code that uses the standard python library, it helps to read the docs. Search for "threading" and read away.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
  • So from where is .send() inherited ? Sockets i think please explain. – Shubham Nov 10 '14 at 23:30
  • line 214 `th=FTPserverThread(self.sock.accept())` creates the thread and passes in 1 argument - the result of calling `self.sock.accept`. `self.sock` is a socket and the [socket documentation](https://docs.python.org/3/library/socket.html?highlight=socket#module-socket) tells you that it returns a (conn, address) tuple, _where conn is a new socket object_. So, going back to the socket documentation, you see it has a `send` method. – tdelaney Nov 10 '14 at 23:53