473,614 Members | 2,270 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_STR EAM)
else:
self.sock = sock
def connect(host, port):
self.sock.conne ct((host, port))
def mysend(msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send( msg[totalsent:])
if sent == 0:
raise RuntimeError, \\
"connession e socket interrotta"
totalsent = totalsent + sent
def myreceive():
msg = ''
while len(msg) < MSGLEN:
chunk = self.sock.recv( MSGLEN-len(msg))
if chunk == '':
raise RuntimeError, \\
"connession e socket interrotta"
msg = msg + chunk
return msg

How can i use this?
Thanks all!
Marco

Jan 16 '06 #1
4 3182
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_STR EAM)
else:
self.sock = sock
def connect(host, port):
self.sock.conne ct((host, port))
def mysend(msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send( msg[totalsent:])
if sent == 0:
raise RuntimeError, \\
"connession e socket interrotta"
totalsent = totalsent + sent
def myreceive():
msg = ''
while len(msg) < MSGLEN:
chunk = self.sock.recv( MSGLEN-len(msg))
if chunk == '':
raise RuntimeError, \\
"connession e 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.c om.

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_STR EAM)
else:
self.sock = sock
def connect(host, port):
self.sock.conne ct((host, port))
def mysend(msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send( msg[totalsent:])
if sent == 0:
raise RuntimeError, \\
"connession e 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, \\
"connession e 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_STR EAM)
else:
self.sock = sock
def connect(host, port):
self.sock.conne ct((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
1697
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/] ________________________________________________________________________ Yahoo! India Insurance Special: Be informed on the best policies, services, tools and more. Go to: http://in.insurance.yahoo.com/licspecial/index.html
1
1566
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 time. Does someone know where I can find those documents/tutorials ? Very thanks, K.
2
15322
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 now see the socket.RecieveTimeout 'property' in the visual studio 2005 help documentation (framework 2.0) and it has example of it being used with TCP socket. This propery is also listed as 'new in .net 2.0'. 1) is the socket.RecieveTimeout...
1
145
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 module, though, as Twisted provides a higher-level interface to network programming. You can find some introductory documentation about writing clients here: http://twistedmatrix.com/projects/core/documentation/howto/clients.html
0
8130
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8623
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8576
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8275
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8429
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7091
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6088
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4050
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1745
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.