Socket Programming HOWTO example

M

Marco Meoni

Hi. I read the Gordon McMillan's "Socket Programming Howto".
I tried to use the example in this howto but this doesn't work.
The code is class mysocket:
'''classe solamente dimostrativa
- codificata per chiarezza, non per efficenza'''
def __init__(self, sock=None):
if sock is None:
self.sock = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
def connect(host, port):
self.sock.connect((host, port))
def mysend(msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError, \\
"connessione socket interrotta"
totalsent = totalsent + sent
def myreceive():
msg = ''
while len(msg) < MSGLEN:
chunk = self.sock.recv(MSGLEN-len(msg))
if chunk == '':
raise RuntimeError, \\
"connessione socket interrotta"
msg = msg + chunk
return msg

How can i use this?
Thanks all!
Marco
 
S

Steve Holden

Marco said:
Hi. I read the Gordon McMillan's "Socket Programming Howto".
I tried to use the example in this howto but this doesn't work.
The code is class mysocket:
'''classe solamente dimostrativa
- codificata per chiarezza, non per efficenza'''
def __init__(self, sock=None):
if sock is None:
self.sock = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
def connect(host, port):
self.sock.connect((host, port))
def mysend(msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError, \\
"connessione socket interrotta"
totalsent = totalsent + sent
def myreceive():
msg = ''
while len(msg) < MSGLEN:
chunk = self.sock.recv(MSGLEN-len(msg))
if chunk == '':
raise RuntimeError, \\
"connessione socket interrotta"
msg = msg + chunk
return msg

How can i use this?

Well, a lot depends on what you mean by "doesn't work".

I can see you have changed the example a little (because I know that
Gordon's original didn't have comments in Italian). Did the program
produce a syntax error, a traceback or what? It would have been helpful
if you had included a link to Gordon's tutorial -- I presume you mean
the one at

http://www.amk.ca/python/howto/sockets/

What are you trying to do, and why do you think this particular chunk of
code might help you? Did you actually try to create and use any
instances of this class?

regards
Steve
 
B

Bryan Olson

Marco said:
Hi. I read the Gordon McMillan's "Socket Programming Howto".
I tried to use the example in this howto but this doesn't work.

You are right, that obviously won't work. The code passes
'self' to __init__, but not to any of the others methods.

I'm cc'ing this post to (e-mail address removed).

The code is
> class mysocket:
'''classe solamente dimostrativa
- codificata per chiarezza, non per efficenza'''
def __init__(self, sock=None):
if sock is None:
self.sock = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
def connect(host, port):
self.sock.connect((host, port))
def mysend(msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError, \\
"connessione socket interrotta"
totalsent = totalsent + sent

To send exactly MSGLEN bytes, use socket's 'sendall' method.
def myreceive():
msg = ''
while len(msg) < MSGLEN:
chunk = self.sock.recv(MSGLEN-len(msg))
if chunk == '':
raise RuntimeError, \\
"connessione socket interrotta"
msg = msg + chunk
return msg
How can i use this?

Treat it as a "HowNotTo".
 
B

Bryan Olson

I mis-phrased:
The code passes
'self' to __init__, but not to any of the others methods.

Of course I meant that the formal parameter for self is missing.
[...]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,065
Latest member
OrderGreenAcreCBD

Latest Threads

Top