473,386 Members | 1,720 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Socket Programming HOWTO example

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

Jan 16 '06 #1
4 3152
Marco Meoni wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Jan 16 '06 #2
Steve Holden ha scritto:
[...]
I can see you have changed the example a little (because I know that
Gordon's original didn't have comments in Italian).


The example cames from italian translation of the howto:
http://python.it/doc/howto/Socket/so...ockets-it.html


Regards Manlio Perillo
Jan 17 '06 #3
Marco Meoni wrote:
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 gm**@hypernet.com.

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".
--
--Bryan
Jan 17 '06 #4
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.
> 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):

[...]
Jan 17 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Premshree Pillai | last post by:
For the uninitiated: "Socket Programming in Python" at evolt.org: http://evolt.org/article/Socket_Programming_in_Python/17/60276/index.html ===== -Premshree http://www.qiksearch.com/] ...
1
by: le dahut | last post by:
Hi, I've read the Gordon McMillan's "Socket Programming HOWTO" and I'm looking for other documents that show examples on how to write a socket server that can answer multiple clients at the same...
2
by: djc | last post by:
I read a network programming book (based on framework 1.1) which indicated that you should 'never' use the RecieveTimeout or the SendTimeout 'socket options' on TCP sockets or you may loose data. I...
1
by: Jean-Paul Calderone | last post by:
On Mon, 12 May 2008 09:19:48 -0700 (PDT), petr.poupa@gmail.com wrote: I'm not sure what you want to happen, so I can't write it for you. I suggest you start with Twisted instead of the socket...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.