Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #62578 > unrolled thread

python socket query

Started bysmilesonisamal@gmail.com
First post2013-12-22 19:05 -0800
Last post2013-12-23 14:23 +0100
Articles 5 — 3 participants

Back to article view | Back to comp.lang.python


Contents

  python socket query smilesonisamal@gmail.com - 2013-12-22 19:05 -0800
    Re: python socket query Chris Angelico <rosuav@gmail.com> - 2013-12-23 14:19 +1100
      Re: python socket query smilesonisamal@gmail.com - 2013-12-23 19:33 -0800
        Re: python socket query Chris Angelico <rosuav@gmail.com> - 2013-12-24 14:54 +1100
    Re: python socket query Piet van Oostrum <piet@vanoostrum.org> - 2013-12-23 14:23 +0100

#62578 — python socket query

Fromsmilesonisamal@gmail.com
Date2013-12-22 19:05 -0800
Subjectpython socket query
Message-ID<3cea84e7-bfd5-4feb-b786-96a3501ffcf4@googlegroups.com>
Hi,
   I am trying to write a TCP socket program in python. I am using python 2.6 in linux. 

I referred following link:
http://www.ibm.com/developerworks/linux/tutorials/l-pysocks/section4.html
I am actually writing the client-side stream socket.
I wrote a small program which creates the socket, bind to the socket, connect to socket and send() close(). I see that there is no reply coming from server and the TCP disconnect happens.
import socket

def tcp(host, request, port=34567):

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.connect((host, port))

s.send(request)

reply = s.recv(2**14)

s.close()

return reply

My problem is even if the request is sent the length(reply) is is 0. I tried to put a timeout of 1 sec s.settimeout() call after the send call but it doesnot help.

I tried by commenting s.close() still it did not work.

Any idea what is the problem?

Regards

Pradeep

[toc] | [next] | [standalone]


#62582

FromChris Angelico <rosuav@gmail.com>
Date2013-12-23 14:19 +1100
Message-ID<mailman.4524.1387768779.18130.python-list@python.org>
In reply to#62578
On Mon, Dec 23, 2013 at 2:05 PM,  <smilesonisamal@gmail.com> wrote:
> I wrote a small program which creates the socket, bind to the socket, connect to socket and send() close(). I see that there is no reply coming from server and the TCP disconnect happens.
> import socket
>
> def tcp(host, request, port=34567):
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>
> s.connect((host, port))
>
> s.send(request)
>
> reply = s.recv(2**14)
>
> s.close()
>
> return reply

First off, your formatting has become mangled. This is likely to be
because of Google Groups, which tends to make a mess of posts. I
strongly recommend you get a better newsreader, such as Thunderbird,
or switch to the mailing list:

https://mail.python.org/mailman/listinfo/python-list

I'm going to assume that (a) the code you've provided is all part of
the tcp() function, and (b) that you are actually calling tcp()
somewhere and seeing what comes back. But once you sort out your
posting issues, you may want to post a complete program (probably not
more than a couple of additional lines beyond what you have above) so
we know what's actually going on.

Terminology point: You mention binding to the socket. In networking,
"bind" has a specific meaning - binding to an address, usually done
for servers, and something you're not doing here.

Are you sure the server's doing something? I tried what you had there
(albeit under Python 3.3), and it seems to be fine. Perhaps you need
to terminate the request with something - maybe a newline. The send()
method will send exactly the bytes you give it, nothing more.

Can you telnet to the server?

ChrisA

[toc] | [prev] | [next] | [standalone]


#62664

Fromsmilesonisamal@gmail.com
Date2013-12-23 19:33 -0800
Message-ID<bc97bf22-8091-41ca-8e86-5a044672a6be@googlegroups.com>
In reply to#62582
On Monday, December 23, 2013 8:49:30 AM UTC+5:30, Chris Angelico wrote:
> On Mon, Dec 23, 2013 at 2:05 PM,  <smilesonisamal@gmail.com> wrote:
> 
> > I wrote a small program which creates the socket, bind to the socket, connect to socket and send() close(). I see that there is no reply coming from server and the TCP disconnect happens.
> 
> > import socket
> 
> >
> 
> > def tcp(host, request, port=34567):
> 
> >
> 
> > s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> 
> >
> 
> > s.connect((host, port))
> 
> >
> 
> > s.send(request)
> 
> >
> 
> > reply = s.recv(2**14)
> 
> >
> 
> > s.close()
> 
> >
> 
> > return reply
> 
> 
> 
> First off, your formatting has become mangled. This is likely to be
> 
> because of Google Groups, which tends to make a mess of posts. I
> 
> strongly recommend you get a better newsreader, such as Thunderbird,
> 
> or switch to the mailing list:
> 
> 
> 
> https://mail.python.org/mailman/listinfo/python-list
> 
> 
> 
> I'm going to assume that (a) the code you've provided is all part of
> 
> the tcp() function, and (b) that you are actually calling tcp()
> 
> somewhere and seeing what comes back. But once you sort out your
> 
> posting issues, you may want to post a complete program (probably not
> 
> more than a couple of additional lines beyond what you have above) so
> 
> we know what's actually going on.
> 
> 
> 
> Terminology point: You mention binding to the socket. In networking,
> 
> "bind" has a specific meaning - binding to an address, usually done
> 
> for servers, and something you're not doing here.
> 
> 
> 
> Are you sure the server's doing something? I tried what you had there
> 
> (albeit under Python 3.3), and it seems to be fine. Perhaps you need
> 
> to terminate the request with something - maybe a newline. The send()
> 
> method will send exactly the bytes you give it, nothing more.
> 
> 
> 
> Can you telnet to the server?
> 
> 
> 
Thanks Chris.I have put bind call but it did not work.  
Even telnet hangs if i tried to connect to the same IP and port manually. Is there a workaround to fix this issue?

If the socket gets closed at the other end. How can we get around with the issue? Any idea?
> ChrisA

[toc] | [prev] | [next] | [standalone]


#62666

FromChris Angelico <rosuav@gmail.com>
Date2013-12-24 14:54 +1100
Message-ID<mailman.4584.1387857301.18130.python-list@python.org>
In reply to#62664
On Tue, Dec 24, 2013 at 2:33 PM,  <smilesonisamal@gmail.com> wrote:
> Thanks Chris.I have put bind call but it did not work.
> Even telnet hangs if i tried to connect to the same IP and port manually. Is there a workaround to fix this issue?
>
> If the socket gets closed at the other end. How can we get around with the issue? Any idea?

Normally, bind isn't necessary for client connections. It's almost
never significant, unless you have multiple IP addresses or you
actually need to specify the port.

If telnet hangs, you need to figure out what your server is doing.
Without knowing the server at all, I can't really help.

But please, have a look at how your post comes out:

https://mail.python.org/pipermail/python-list/2013-December/663308.html

All those messy blank lines are because of Google Groups. There are
other problems, as well. Please, can you switch to a better client, so
we don't have to wade through that mess? Mozilla Thunderbird is one
good option, or you can join the mailing list:

https://mail.python.org/mailman/listinfo/python-list

Thanks!

ChrisA

[toc] | [prev] | [next] | [standalone]


#62625

FromPiet van Oostrum <piet@vanoostrum.org>
Date2013-12-23 14:23 +0100
Message-ID<m2y53bn1kj.fsf@cochabamba.vanoostrum.org>
In reply to#62578
smilesonisamal@gmail.com writes:

> Hi,
>    I am trying to write a TCP socket program in python. I am using python 2.6 in linux. 
>
> I referred following link:
> http://www.ibm.com/developerworks/linux/tutorials/l-pysocks/section4.html
> I am actually writing the client-side stream socket.
> I wrote a small program which creates the socket, bind to the socket, connect to socket and send() close(). I see that there is no reply coming from server and the TCP disconnect happens.
> import socket
>
> def tcp(host, request, port=34567):
>
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>
> s.connect((host, port))
>
> s.send(request)
>
> reply = s.recv(2**14)
>
> s.close()
>
> return reply
>
> My problem is even if the request is sent the length(reply) is is 0. I tried to put a timeout of 1 sec s.settimeout() call after the send call but it doesnot help.
>
> I tried by commenting s.close() still it did not work.
>
> Any idea what is the problem?

Length(reply) == 0 means that the other side closed the socket without sending anything back.
-- 
Piet van Oostrum <piet@vanoostrum.org>
WWW: http://pietvanoostrum.com/
PGP key: [8DAE142BE17999C4]

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web