How to end TCP socket data while using readline()?
arjun.chennu at gmail.com
Mon Mar 1 16:35:38 CET 2010
Thanks for the feedback.
Opening a separate file-obj for writing and for reading is just what I've
been trying, but I don't seem to get it to work. I'm new to python and I'm
not sure if I'm missing the intricacy of some command. Please help:
Here is my server snippet:
(conn, addr) = sock1.accept() # connected socket
print 'Client (localhost) port: ', addr
cf = conn.makefile('r',0) # file obj for reading
lf = open('ccs.txt','w')
for linenum, line in enumerate(cf): # iterate over socket
stat = 'wrote %s lines to file.\n' %(linenum+1)
cff = conn.makefile('w',0) # file obj for writing
cff.writelines(stat) # cff.write(stat) does not work
print stat, "DONE!"
And here is the client that I have for it: (sfp is the local file object i
for line in sfp.readlines():
print 'done sending'
cf.close() #writing ends here
cff = s.makefile('r',0) # file obj for writing
for line in cff.readlines():
The execution sends all the lines (and prints out the len(line) ) and then
stays waiting. THen when I manually terminate the client script, the server
script happily types the "DONE!" output.
Where is this protocol hanging up? Help much appreciated, with a small
On Sat, Feb 27, 2010 at 05:11, Cameron Simpson <cs at zip.com.au> wrote:
> On 26Feb2010 10:39, Arjun <arjun.chennu at gmail.com> wrote:
> | Hi, I have a small script that runs a TCP server. A client connects to
> | this server and transmits a stored file line-by-line, and then waits
> | for a confirmation "done". However, when I run them the first loop
> | never really ends -- as the TCP server keeps expecting more data. I am
> | using a file-like-object, and so somehow I have to communicate to the
> | server that it is the end-of-file.
> | here is some server code
> | <snip>
> | sock1.bind(('', port))
> | print "Listening at port: ", port
> | sock1.listen(1) # listening socket
> | (conn, addr) = sock1.accept() # connected socket
> | print 'Client (localhost) port: ', addr
> | cf = conn.makefile('r',0) # file like obj for socket
> | print 'close'
> | cf.flush()
> | cf.close()
> | sfp.close()
> Too many files. It's not that hard! Or shouldn't be.
> | So what I am wondering is:
> | 1. Using a file-like object means that the socket becomes uni-
> | directional, until the mode of the file object is changed from 'r' to
> | 'w' (or vice versa). This seems inefficient, and rather unPythonesque.
> | Can somebody tell me if there is a more elegant way of receiving all
> | the lines from the client, and then sending a "done" message to the
> | client?
> Get the socket. It is a file descriptor (or in may be a python "socket"
> object with a file descriptor inside).
> Open _two_ "file" objects on it using
> from_file = os.fdopen(fd_of_socket, "r")
> to_file = os.fdopen(fd_of_socket, "w").
> Use the same:
> print >>to_file, 'close'
> method as you're using already.
> Read from to_file as needed.
> The same scheme should work in both server and client:
> Don't look for EOF, watch the input line flow.
> You might need to use readline() instead of the file-by-line iteration
> which I seem to recall has some sort of problem handing out the "latest"
> Cameron Simpson <cs at zip.com.au> DoD#743
> It's better, when you're riding with someone you don't know so well, to
> to the inside line - it's easier to avoid the bits...
> - Barry Sheene
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Python-list