python socket query

S

smilesonisamal

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
 
C

Chris Angelico

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
 
P

Piet van Oostrum

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.
 
S

smilesonisamal

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?
 
C

Chris Angelico

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top