#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2004
    Posts
    26
    Rep Power
    0

    my socket class: getting a strange error regarding my methods


    okay, so the code is below. when i run it coupled with the following test code, I get the following error.
    PHP Code:
    import socket

    class NewSock:
        
    "a class intended to simplify socket programming"
        
    # derived from the class in the HOWTO documents
        
    def __init__self sock None ):
            if 
    sock is None:
                
    self.sock socket.socketsocket.AF_INET socket.SOCK_STREAM )
            else:
                
    self.sock sock
        def connect
    host port ):
            try:
                
    self.sock.connect( ( host port ) )
            
    except error:
                return 
    0
            
    return 1
        def bind
    host port ):
            try:
                
    self.sock.bind( ( host port ) )
            
    except error:
                return 
    0
            
    return 1
        def run
    ():
            
    self.sock.listen(5)
            
    self.conn self.addr self.sock.accept()
            
    # i know this doesn't do anything yet, i'm going to
            # implement threads as soon as i get this class
            # working
        
    def senddata ):
            
    datlen lendata )
            
    totalsent 0
            
    while totalsent datlen:
                
    sent self.conn.sendmsg[totalsent:] )
                if 
    sent == 0:
                    return 
    0;
                
    totalsent += sent
            
    else:
                return 
    1
        def recv
    ():
            
    '''
            okay, so the first recv() loop is meant to gather the initial data,
            which will be an integer that tells the second recv() loop how much
            data to expect this time around. prebytsz hold this, and the \n
            tells the first loop when they have aquired the byte size. for example:
            "45\nthismessageisaslongasthenumberbeforethereturn"
            for this reason, i take the data byte by byte, waiting for the \n.
            then i transfer the data over to the variable "remaining", minus the
            last character which, ideally, is a \n, where it is converted to an
            integer. as data comes in their len()s are subtracted from remaining,
            until its all gone. sounds good on paper, but will it work?
            '''
            
    prebytsz ''
            
    while prebytsz[0:lenprebytsz )] != "\n":
                
    prebytsz += self.conn.recv)
            else:
                if 
    prebytsz[0:lenprebytsz )] is "\n":
                    
    data ''
                    
    remaining intprebytszlenprebytsz ) - ] )
                    while 
    lendata ) < remaining:
                        
    data += self.conn.recv1024 )
                        
    remaining -= lendata )
                    return 
    data
                
    else:
                    return 

    PHP Code:
    sock NewSock

    sock
    .bind'localhost' 5376 )

    sock.run()

    sock.send"I HATE YOU: " sock.recv() ) 
    Code:
    Traceback (most recent call last):
      File "C:\Documents and Settings\Daniel\Desktop\python\class_socket.py", line 67, in ?
        sock.bind( 'localhost' , 5376 )
    TypeError: unbound method bind() must be called with NewSock instance as first argument (got str instance instead)
    I'm not sure what to do, at all, could someone help? Thanks.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2004
    Location
    Canada
    Posts
    242
    Rep Power
    0
    You're missing the parentheses. You need to use

    Code:
     sock = NewSock()
    to create an instance of NewSock.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    34
    Rep Power
    11
    oh! omfg, my mistake. thanks alot
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    34
    Rep Power
    11
    damnit, now i get this error:
    Code:
    Traceback (most recent call last):
      File "C:\Documents and Settings\Daniel\Desktop\python\class_socket.py", line 67, in ?
        sock.bind('localhost',5376)
    TypeError: bind() takes exactly 2 arguments (3 given)
    that can't be right, look at the code

    (oh, ignore the multiple usernames. the software screwed up my other one.)
  8. #5
  9. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    When you define a method you need to add an extra paramater before your arguments, which will refer to that class instance itself. In Python this is called self, though technically you can use any name you want. But its a good idea to follow the standards where you can .

    So.
    Code:
    def bind( host , port ):
    becomes,
    Code:
    def bind(self, host, port):
    etc.

    Hope this helps,

    Mark.
    Last edited by netytan; July 27th, 2004 at 08:08 AM.
    programming language development: www.netytan.com Hula


IMN logo majestic logo threadwatch logo seochat tools logo