Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old July 26th, 2004, 11:48 AM
Beeyah Beeyah is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 26 Beeyah User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation 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.

Reply With Quote
  #2  
Old July 26th, 2004, 01:37 PM
NetBSD NetBSD is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Apr 2004
Location: Canada
Posts: 234 NetBSD Negative: is most likely a SPAMMER and a traitor to the cause. 
Time spent in forums: 9 h 25 m 35 sec
Reputation Power: 0
Send a message via MSN to NetBSD
You're missing the parentheses. You need to use

Code:
 sock = NewSock()
 


to create an instance of NewSock.

Reply With Quote
  #3  
Old July 26th, 2004, 01:43 PM
dbickett dbickett is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 34 dbickett User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
oh! omfg, my mistake. thanks alot

Reply With Quote
  #4  
Old July 26th, 2004, 01:47 PM
dbickett dbickett is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 34 dbickett User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 5
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.)

Reply With Quote
  #5  
Old July 27th, 2004, 06:58 AM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,536 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 18 h 3 m 4 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.
__________________
programming language development: www.netytan.com Hula


Last edited by netytan : July 27th, 2004 at 07:08 AM.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > my socket class: getting a strange error regarding my methods


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
Stay green...Green IT