reading from sockets

AndrewTK atkedzierski at gmail.com
Thu Aug 10 12:34:55 EDT 2006


Hello,

I'm trying to read data from a socket and I'm not seeing what I'm
expecting.... it seems to skip the first line of data. I am new to
Python and just trying to test what I can do with it... and it's not
looking pretty.


I have some Python code:
[------------------------------
#! /usr/bin/python

import socket

s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect( ("localhost",54321) );
s.send("hello")
data = s.recv(1024)
while len(data) > 0:
	print repr(data) # the first print of data always seems to "ignore"
the first line of data...
	data = s.recv(1024)
------------------------------]

On localhost 54321 I have a server (in Java) writing out

[------------------------------
first
second
third
<EOF>
------------------------------]
(on EOF, the stream is simply closed.)

The result of the script is to print

[------------------------------
second
third
------------------------------]

I have tried already
[++++++++++++++
data = " "
while len(data) > 0:
	data = s.recv(1024)
	print repr(data)
++++++++++++++]

but that has not changed anything. The first line to come through is
always skipped it would seem.

Any ideas as to why that would be....? I was thinking scope when I made
the last example, but apparently that is not the case.

Andrew




More information about the Python-list mailing list