Re: Socket Programming HOWTO example
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): [...] -- http://mail.python.org/mailman/listinfo/python-list
Re: Socket Programming HOWTO example
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 [EMAIL PROTECTED] > 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: Socket Programming HOWTO example
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/sockets-it/sockets-it.html Regards Manlio Perillo -- http://mail.python.org/mailman/listinfo/python-list
Re: Socket Programming HOWTO example
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/ -- http://mail.python.org/mailman/listinfo/python-list
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 -- http://mail.python.org/mailman/listinfo/python-list