Broken pipe

Discussion in 'Python' started by cerr, May 7, 2010.

  1. cerr

    cerr Guest

    Hi There,

    I'm very new to Python and i wanna write a script that sends a certain
    string to a server. The code I came up with looks like this:
    #!/usr/bin/python

    import sys
    import string

    from socket import *
    usage="USAGE: "+sys.argv[0]+" <server> <port>";
    if len(sys.argv) != 3:
    print usage;
    sys.exit(0);
    host = sys.argv[1];
    port = sys.argv[2];
    buf = 1024;
    addr = (host,port);
    sock = socket(AF_INET, SOCK_STREAM);
    data = string.join("NovaxTest",'\n');
    sock.send(data);
    sock.close();
    and I'm calling this script like that: "./TestService.py 127.0.0.1
    1514" but when I call it I get following back:
    sending data to 127.0.0.1:1514
    data: NovaxTest
    Traceback (most recent call last):
    File "./TestService.py", line 18, in <module>
    sock.send(data);
    socket.error: [Errno 32] Broken pipe
    I understand that UNIX sends an Errno32 if the server closes the
    connection. But if i telnet to localhost on 1514 and send NovaxTest by
    hand everything works just fine. So what might be wrong here?

    Thank you very much!
    Ron
    cerr, May 7, 2010
    #1
    1. Advertising

  2. cerr

    Chris Rebert Guest

    On Thu, May 6, 2010 at 10:27 PM, cerr <> wrote:
    > Hi There,
    >
    > I'm very new to Python and i wanna write a script that sends a certain
    > string to a server. The code I came up with looks like this:
    > #!/usr/bin/python
    >
    > import sys
    > import string
    >
    > from socket import *
    > usage="USAGE: "+sys.argv[0]+" <server> <port>";
    > if len(sys.argv) != 3:
    >              print usage;
    >              sys.exit(0);
    > host = sys.argv[1];
    > port = sys.argv[2];
    > buf = 1024;
    > addr = (host,port);
    > sock = socket(AF_INET, SOCK_STREAM);
    > data = string.join("NovaxTest",'\n');
    > sock.send(data);
    > sock.close();
    > and I'm calling this script like that: "./TestService.py 127.0.0.1
    > 1514" but when I call it I get following back:
    > sending data to 127.0.0.1:1514
    > data: NovaxTest
    > Traceback (most recent call last):
    >  File "./TestService.py", line 18, in <module>
    >    sock.send(data);
    > socket.error: [Errno 32] Broken pipe
    > I understand that UNIX sends an Errno32 if the server closes the
    > connection. But if i telnet to localhost on 1514 and send NovaxTest by
    > hand everything works just fine. So what might be wrong here?


    You never called sock.connect(addr). Your code doesn't even use `addr` at all.
    Also, please don't use semicolons in your code. It's bad style.

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, May 7, 2010
    #2
    1. Advertising

  3. cerr

    Ron Eggler Guest

    On May 6, 2010 10:37:14 pm Chris Rebert wrote:
    > On Thu, May 6, 2010 at 10:27 PM, cerr <> wrote:
    > > Hi There,
    > >
    > > I'm very new to Python and i wanna write a script that sends a certain
    > > string to a server. The code I came up with looks like this:
    > > #!/usr/bin/python
    > >
    > > import sys
    > > import string
    > >
    > > from socket import *
    > > usage="USAGE: "+sys.argv[0]+" <server> <port>";
    > > if len(sys.argv) != 3:
    > > print usage;
    > > sys.exit(0);
    > > host = sys.argv[1];
    > > port = sys.argv[2];
    > > buf = 1024;
    > > addr = (host,port);
    > > sock = socket(AF_INET, SOCK_STREAM);
    > > data = string.join("NovaxTest",'\n');
    > > sock.send(data);
    > > sock.close();
    > > and I'm calling this script like that: "./TestService.py 127.0.0.1
    > > 1514" but when I call it I get following back:
    > > sending data to 127.0.0.1:1514
    > > data: NovaxTest
    > > Traceback (most recent call last):
    > > File "./TestService.py", line 18, in <module>
    > > sock.send(data);
    > > socket.error: [Errno 32] Broken pipe
    > > I understand that UNIX sends an Errno32 if the server closes the
    > > connection. But if i telnet to localhost on 1514 and send NovaxTest by
    > > hand everything works just fine. So what might be wrong here?

    >
    > You never called sock.connect(addr). Your code doesn't even use `addr` at
    > all.

    Oh, yeah, hOOps :$

    > Also, please don't use semicolons in your code. It's bad style.

    Is it, eh? Well, I'm from C, C++ and for me it just belongs there..:) but i'll
    try to change my habits... :)

    Hm weird now I get something like:
    Traceback (most recent call last):
    File "./TestService.py", line 14, in <module>
    sock.connect((host,port))
    File "<string>", line 1, in connect
    TypeError: an integer is required


    with this code:

    #!/usr/bin/python

    import sys
    import string

    from socket import *
    usage="USAGE: "+sys.argv[0]+" <server> <port>"
    if len(sys.argv) != 3:
    print usage
    sys.exit(0)
    host = sys.argv[1]
    port = sys.argv[2]
    sock = socket(AF_INET, SOCK_STREAM)
    sock.connect((host,port))
    data = string.join("NovaxTest",'\n')
    sock.send(data)
    sock.close()

    What does that mean? :(

    Thanks,
    Ron
    Ron Eggler, May 7, 2010
    #3
  4. cerr

    Chris Rebert Guest

    On Thu, May 6, 2010 at 11:11 PM, Ron Eggler <> wrote:
    > On May 6, 2010 10:37:14 pm Chris Rebert wrote:
    >> On Thu, May 6, 2010 at 10:27 PM, cerr <> wrote:
    >> > Hi There,
    >> >
    >> > I'm very new to Python and i wanna write a script that sends a certain
    >> > string to a server. The code I came up with looks like this:
    >> > #!/usr/bin/python
    >> >
    >> > import sys
    >> > import string
    >> >
    >> > from socket import *
    >> > usage="USAGE: "+sys.argv[0]+" <server> <port>";
    >> > if len(sys.argv) != 3:
    >> >              print usage;
    >> >              sys.exit(0);
    >> > host = sys.argv[1];
    >> > port = sys.argv[2];
    >> > buf = 1024;
    >> > addr = (host,port);
    >> > sock = socket(AF_INET, SOCK_STREAM);
    >> > data = string.join("NovaxTest",'\n');
    >> > sock.send(data);
    >> > sock.close();
    >> > and I'm calling this script like that: "./TestService.py 127.0.0.1
    >> > 1514" but when I call it I get following back:
    >> > sending data to 127.0.0.1:1514
    >> > data: NovaxTest
    >> > Traceback (most recent call last):
    >> >  File "./TestService.py", line 18, in <module>
    >> >    sock.send(data);
    >> > socket.error: [Errno 32] Broken pipe
    >> > I understand that UNIX sends an Errno32 if the server closes the
    >> > connection. But if i telnet to localhost on 1514 and send NovaxTest by
    >> > hand everything works just fine. So what might be wrong here?

    >>
    >> You never called sock.connect(addr). Your code doesn't even use `addr` at
    >> all.

    > Oh, yeah, hOOps :$

    <snip>
    > Hm weird now I get something like:
    > Traceback (most recent call last):
    >  File "./TestService.py", line 14, in <module>
    >    sock.connect((host,port))
    >  File "<string>", line 1, in connect
    > TypeError: an integer is required

    <snip>
    > What does that mean? :(


    You never converted `port` to an int, it's still a string.

    You might consider reading the socket module docs:
    http://docs.python.org/library/socket.html

    Cheers,
    Chris
    --
    http://blog.rebertia.com
    Chris Rebert, May 7, 2010
    #4
  5. cerr

    Ron Eggler Guest

    --
    Ron Eggler
    Suite# 1804
    1122 Gilford St
    Vancouver, BC V6G 2P5
    Canada
    (778) 230-9442
    > On Thu, May 6, 2010 at 11:11 PM, Ron Eggler <> wrote:
    > > On May 6, 2010 10:37:14 pm Chris Rebert wrote:
    > >> On Thu, May 6, 2010 at 10:27 PM, cerr <> wrote:
    > >> > Hi There,
    > >> >
    > >> > I'm very new to Python and i wanna write a script that sends a certain
    > >> > string to a server. The code I came up with looks like this:
    > >> > #!/usr/bin/python
    > >> >
    > >> > import sys
    > >> > import string
    > >> >
    > >> > from socket import *
    > >> > usage="USAGE: "+sys.argv[0]+" <server> <port>";
    > >> > if len(sys.argv) != 3:
    > >> > print usage;
    > >> > sys.exit(0);
    > >> > host = sys.argv[1];
    > >> > port = sys.argv[2];
    > >> > buf = 1024;
    > >> > addr = (host,port);
    > >> > sock = socket(AF_INET, SOCK_STREAM);
    > >> > data = string.join("NovaxTest",'\n');
    > >> > sock.send(data);
    > >> > sock.close();
    > >> > and I'm calling this script like that: "./TestService.py 127.0.0.1
    > >> > 1514" but when I call it I get following back:
    > >> > sending data to 127.0.0.1:1514
    > >> > data: NovaxTest
    > >> > Traceback (most recent call last):
    > >> > File "./TestService.py", line 18, in <module>
    > >> > sock.send(data);
    > >> > socket.error: [Errno 32] Broken pipe
    > >> > I understand that UNIX sends an Errno32 if the server closes the
    > >> > connection. But if i telnet to localhost on 1514 and send NovaxTest by
    > >> > hand everything works just fine. So what might be wrong here?
    > >>
    > >> You never called sock.connect(addr). Your code doesn't even use `addr`
    > >> at all.

    > >
    > > Oh, yeah, hOOps :$

    >
    > <snip>
    >
    > > Hm weird now I get something like:
    > > Traceback (most recent call last):
    > > File "./TestService.py", line 14, in <module>
    > > sock.connect((host,port))
    > > File "<string>", line 1, in connect
    > > TypeError: an integer is required

    >
    > <snip>
    >
    > > What does that mean? :(

    >
    > You never converted `port` to an int, it's still a string.


    port = int(sys.argv[2])

    doesn't seem to help it either :(
    >
    > You might consider reading the socket module docs:
    > http://docs.python.org/library/socket.html


    mh, i browsed through it but didn't quite find what i'm looking for, do you
    have any more hints?

    Thanks,
    Ron
    Ron Eggler, May 7, 2010
    #5
  6. cerr

    cerr Guest

    On May 7, 9:45 am, Ron Eggler <> wrote:
    > --
    > Ron Eggler
    > Suite# 1804
    > 1122 Gilford St
    > Vancouver, BC V6G 2P5
    > Canada
    > (778) 230-9442
    >
    >
    >
    >
    >
    > > On Thu, May 6, 2010 at 11:11 PM, Ron Eggler <> wrote:
    > > > On May 6, 2010 10:37:14 pm Chris Rebert wrote:
    > > >> On Thu, May 6, 2010 at 10:27 PM, cerr <> wrote:
    > > >> > Hi There,

    >
    > > >> > I'm very new to Python and i wanna write a script that sends a certain
    > > >> > string to a server. The code I came up with looks like this:
    > > >> > #!/usr/bin/python

    >
    > > >> > import sys
    > > >> > import string

    >
    > > >> > from socket import *
    > > >> > usage="USAGE: "+sys.argv[0]+" <server> <port>";
    > > >> > if len(sys.argv) != 3:
    > > >> >              print usage;
    > > >> >              sys.exit(0);
    > > >> > host = sys.argv[1];
    > > >> > port = sys.argv[2];
    > > >> > buf = 1024;
    > > >> > addr = (host,port);
    > > >> > sock = socket(AF_INET, SOCK_STREAM);
    > > >> > data = string.join("NovaxTest",'\n');
    > > >> > sock.send(data);
    > > >> > sock.close();
    > > >> > and I'm calling this script like that: "./TestService.py 127.0.0.1
    > > >> > 1514" but when I call it I get following back:
    > > >> > sending data to 127.0.0.1:1514
    > > >> > data: NovaxTest
    > > >> > Traceback (most recent call last):
    > > >> >  File "./TestService.py", line 18, in <module>
    > > >> >    sock.send(data);
    > > >> > socket.error: [Errno 32] Broken pipe
    > > >> > I understand that UNIX sends an Errno32 if the server closes the
    > > >> > connection. But if i telnet to localhost on 1514 and send NovaxTest by
    > > >> > hand everything works just fine. So what might be wrong here?

    >
    > > >> You never called sock.connect(addr). Your code doesn't even use `addr`
    > > >> at all.

    >
    > > > Oh, yeah, hOOps :$

    >
    > > <snip>

    >
    > > > Hm weird now I get something like:
    > > > Traceback (most recent call last):
    > > >  File "./TestService.py", line 14, in <module>
    > > >    sock.connect((host,port))
    > > >  File "<string>", line 1, in connect
    > > > TypeError: an integer is required

    >
    > > <snip>

    >
    > > > What does that mean? :(

    >
    > > You never converted `port` to an int, it's still a string.

    >
    > port = int(sys.argv[2])
    >
    > doesn't seem to help it either :(
    >
    >
    >
    > > You might consider reading the socket module docs:
    > >http://docs.python.org/library/socket.html

    >
    > mh, i browsed through it but didn't quite find what i'm looking for, do you
    > have any more hints?
    >

    Ah, okay, I figured that out! I actually just needed to add a \n at
    the end to signal the server that this is the end of the line. All
    good now, thanks for your help!
    --
    Ron
    cerr, May 7, 2010
    #6
  7. In message <>, Chris
    Rebert wrote:

    > Also, please don't use semicolons in your code. It's bad style.


    Wonder why they’re allowed, then.
    Lawrence D'Oliveiro, May 13, 2010
    #7
  8. cerr

    Lie Ryan Guest

    On 05/13/10 22:41, Lawrence D'Oliveiro wrote:
    > In message <>, Chris
    > Rebert wrote:
    >
    >> Also, please don't use semicolons in your code. It's bad style.

    >
    > Wonder why they’re allowed, then.


    they're there for line continuation, e.g.:

    a = 40; foo(a)

    but in many cases, putting two statements in a single line reduces
    readability so use the semicolons extremely conservatively. But the
    worst is the abuse of semicolons for end-of-line markers.
    Lie Ryan, May 13, 2010
    #8
  9. In message <4bec2a9a$>, Lie Ryan wrote:

    > On 05/13/10 22:41, Lawrence D'Oliveiro wrote:
    >> In message <>, Chris
    >> Rebert wrote:
    >>
    >>> Also, please don't use semicolons in your code. It's bad style.

    >>
    >> Wonder why they’re allowed, then.

    >
    > they're there for line continuation, e.g.:
    >
    > a = 40; foo(a)
    >
    > but in many cases, putting two statements in a single line reduces
    > readability so use the semicolons extremely conservatively. But the
    > worst is the abuse of semicolons for end-of-line markers.


    So why are they allowed, then?
    Lawrence D'Oliveiro, May 15, 2010
    #9
  10. cerr

    Lie Ryan Guest

    On 05/15/10 11:56, Lawrence D'Oliveiro wrote:
    > In message <4bec2a9a$>, Lie Ryan wrote:
    >
    >> On 05/13/10 22:41, Lawrence D'Oliveiro wrote:
    >>> In message <>, Chris
    >>> Rebert wrote:
    >>>
    >>>> Also, please don't use semicolons in your code. It's bad style.
    >>>
    >>> Wonder why they’re allowed, then.

    >>
    >> they're there for line continuation, e.g.:
    >>
    >> a = 40; foo(a)
    >>
    >> but in many cases, putting two statements in a single line reduces
    >> readability so use the semicolons extremely conservatively. But the
    >> worst is the abuse of semicolons for end-of-line markers.

    >
    > So why are they allowed, then?


    Convenience. I've sometimes, in the interactive interpreter, written
    codes that looks like:

    a = 0; ... other code ...

    since I need to reset the variable 'a' every time 'other code' is run.
    If there hasn't been the ;, then I'd have to press up, up, enter, up,
    up, change, enter. Compare that to up, change, enter since I can "up"
    two "lines" at once.

    In the interactive interpreter these sort of conveniences is often useful.

    Unless your "why are they allowed" is about why a blank continuation
    (i.e. abusing ; as line ending) is allowed. In that case, I'd presume
    it's just because conceptually:

    foo();
    bar

    is just:

    foo()

    bar()


    but probably you're right; maybe they should be strictly disallowed. But
    it's too late for this kind of change, not until py4k.
    Lie Ryan, May 15, 2010
    #10
  11. On Sat, 15 May 2010 13:56:08 +1200, Lawrence D'Oliveiro
    <_zealand> declaimed the following in
    gmane.comp.python.general:


    > So why are they allowed, then?


    Semicolon is a statement SEPARATOR -- but so is a NEWLINE.

    Some languages (see C and Ada) use semicolon as a statement
    TERMINATOR (and newline does not end a statement).

    The epitomy of the confusion is Pascal, where a block looking like:

    if...then
    begin
    statement1;
    statement2;
    end;

    actually has THREE statements -- the third statement is a Nil. The
    correct (or acceptable) version would be:

    if...then
    begin
    statement1;
    statement2
    end;


    Ada uses it as a terminator, so the following is valid:

    if ... then
    state
    ment
    1;
    state
    ment
    2;
    end if;

    --
    Wulfraed Dennis Lee Bieber AF6VN
    HTTP://wlfraed.home.netcom.com/
    Dennis Lee Bieber, May 16, 2010
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. funtoosh

    how to detect broken pipe ?

    funtoosh, Dec 20, 2003, in forum: Perl
    Replies:
    1
    Views:
    909
    Ben Morrow
    Dec 21, 2003
  2. Clive
    Replies:
    0
    Views:
    3,005
    Clive
    Aug 12, 2003
  3. Tomas Penc
    Replies:
    3
    Views:
    35,931
  4. lee, wonsun
    Replies:
    1
    Views:
    484
    Jack Klein
    Nov 2, 2004
  5. Replies:
    1
    Views:
    222
    Ben Morrow
    Jun 2, 2004
Loading...

Share This Page