Thread: errors

    #1
  1. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407

    errors


    OK I'm trying to code new stuff in new ways for experience before i can get to code something good.. anyways.. here is my code

    Code:
    class TCP:
          def __init__( self, host, port ):
    
              self = socket( AF_INET, SOCK_STREAM )
              self.connect(( "%s", "%d" )) % ( host, port )
    
              self.send( 'GET /index.php/HTML/1.1 \n\r\n\r' )
              print self.recv( 1025 )
             
             
    if __name__ == '__main__':
    
       print 'Enter host name to connect to'
       print '> '
       connection = raw_input( )
       
       print 'Enter port number'
       ports = raw_input( )
       
       con = TCP( connection, ports )
    and here is my error

    Code:
    Traceback (most recent call last):
      File "scripts/ftp.py", line 23, in ?
        con = TCP( connection, ports )
      File "scripts/ftp.py", line 9, in __init__
        self.connect(( "%s", "%d" )) % ( host, port )
      File "<string>", line 1, in connect
    TypeError: an integer is required
    can anyone tell me why i keep getting that err? i am providing an integer as a port number
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    But your not, you inserting an ind %d into a string, but the value is still a string . Also add the fact that raw_input always returns a string and you have your problem i.e.

    >>> '%d' % 50
    '50'
    >>> raw_input('Number please: ')
    Number please: 50
    '50'
    >>>

    you could replace connect with something like this...

    ...
    self.connect((host, int(port)))
    ...

    Mark.
    programming language development: www.netytan.com Hula

  4. #3
  5. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    hmm thats weird i tested it that way before and it didnt work! hmm thanx again life-saver
  6. #4
  7. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    btw how can this part work

    Code:
    class TCP:
          def __init__( self, host, port ):
    
              self = socket( AF_INET, SOCK_STREAM )
              self.connect(( host, int( port )))
    
              self.send( 'GET /index.php/HTML/1.1 \n\r\n\r' )
    
          def __str__( self ):
    
              print self.recv( 1025 )
  8. #5
  9. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    It's a "free" world. Do what you like but leave self alone or you'll regret it later. The run-time system will send the boys around
    Code:
    >>> class fred:
    	def __init__(self):
    		print "It's me %s"%(self,)
    
    >>> a = fred()
    It's me <__main__.fred instance at 0x00A36328>
    >>> a
    <__main__.fred instance at 0x00A36328>
    >>> a.self
    
    Traceback (most recent call last):
      File "<pyshell#9>", line 1, in -toplevel-
        a.self
    AttributeError: fred instance has no attribute 'self'
    >>>
    Grim.
  10. #6
  11. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    heh? why btw.. can you tell me why my _str__ is not working.. arg it suppost to work
  12. #7
  13. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Hi,
    self is a psuedo variable created by the run-time system and acts as a pointer to the object instance.
    Code:
    class MyClass:
    	def __init__(self, number):
    		self.apples = number
    
    a = MyClass(10)
    b = MyClass (3)
    print a.apples, b.apples
    self is a bit like like the this variable in C++. In the above example both a and b have an attribute called apples, they are both derived from the MyClass but the quantity of apples is unique to each.

    This code works for me:
    Code:
    import socket
    class myTCP:
    	def __init__(self,host,port):
    		print "Making connection..."
    		self.connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    		self.connection.connect((host,port))
    		self.connection.send("GET /index.html/HTML/1.1 \n\r\n\r")
    		print "Sent GET"
    
    	def output(self):
    		return self.connection.recv(1024)
    con = myTCP("www.bbc.co.uk",80)
    print con.output()
    Grim
  14. #8
  15. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    and the string representation:
    Code:
    	def __str__(self):
    		return self.connection.recv(1024)
    con = myTCP("www.bbc.co.uk",80)
    print con
  16. #9
  17. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    ahh cool cool im assuming that connection is a variable
  18. #10
  19. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    Yep!
    self.connection is a variable (attribute) of the con instance of class MyTCP. I could of called it anything I liked of course:
    Code:
    self.my_connection_to_the_server = .....
  20. #11
  21. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Ok, i think i should mention that self is optional so you can use whatever name you like i.e. this... however Python standards are too use self so please do

    Also, Pythons style guide/standard is to NOT put spaces directly inside brackets i.e raw_input( 'something' ), its up to you whether you follow them though

    http://www.python.org/doc/essays/styleguide.html

    X: the reason you need to call int() on port is that connect takes a tuple of (host_as_string, port_as_int). raw_input() returns a string so you have to change its type of connect will barf!

    Mark.
    programming language development: www.netytan.com Hula

  22. #12
  23. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    yes and i reolized that .. just weird that i did it that way before but didnt work.. not it does.. thanx
  24. #13
  25. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    xlordt,
    Not the best code in the world but it shows a way you might synchronise the copy between to PC's. It is not something to build a site on
    You should look into the socket exceptions rather than the slap-dash stuff I've put here. I would really consider doing proper ftp - because although that has more coding overhead it at least handles error situations - this code does not.

    not so Grim
  26. #14
  27. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    14
    files
    Attached Files
  28. #15
  29. Only the strong survives!!.
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Feb 2003
    Location
    A World of wonders.
    Posts
    5,583
    Rep Power
    407
    thanx

IMN logo majestic logo threadwatch logo seochat tools logo