python socket query

smilesonisamal at gmail.com smilesonisamal at gmail.com
Tue Dec 24 04:33:00 CET 2013


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 at 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




More information about the Python-list mailing list