Thanks for the feedback.<br><br>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:<br>
<br>Here is my server snippet:<br><br> (conn, addr) = sock1.accept() # connected socket<br> print 'Client (localhost) port: ', addr[1]<br> <br> cf = conn.makefile('r',0) # file obj for reading<br>
lf = open('ccs.txt','w')<br> <br> for linenum, line in enumerate(cf): # iterate over socket lines <br> lf.write(line) <br> #sys.stdout.write(line)<br>
print len(line)<br> <br> cf.close()<br> <br> stat = 'wrote %s lines to file.\n' %(linenum+1)<br> cff = conn.makefile('w',0) # file obj for writing<br>
cff.writelines(stat) # cff.write(stat) does not work either!!<br> cff.close()<br> <br> lf.close()<br> conn.close()<br> print stat, "DONE!"<br>
<br>And here is the client that I have for it: (sfp is the local file object i read from)<br><br> for line in sfp.readlines():<br> cf.write(line)<br> print len(line)<br><br> print 'done sending'<br>
cf.close() #writing ends here<br> <br> cff = s.makefile('r',0) # file obj for writing <br> for line in cff.readlines():<br> print line<br> <br> cff.close()<br>
<br> sfp.close()<br> s.close()<br><br>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.<br>
<br>Where is this protocol hanging up? Help much appreciated, with a small explanation.<br><br>Cheers,<br>Arjun<br><br><div class="gmail_quote">On Sat, Feb 27, 2010 at 05:11, Cameron Simpson <span dir="ltr"><<a href="mailto:cs@zip.com.au">cs@zip.com.au</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;"><div class="im">On 26Feb2010 10:39, Arjun <<a href="mailto:arjun.chennu@gmail.com">arjun.chennu@gmail.com</a>> wrote:<br>
| Hi, I have a small script that runs a TCP server. A client connects to<br>
| this server and transmits a stored file line-by-line, and then waits<br>
| for a confirmation "done". However, when I run them the first loop<br>
| never really ends -- as the TCP server keeps expecting more data. I am<br>
| using a file-like-object, and so somehow I have to communicate to the<br>
| server that it is the end-of-file.<br>
|<br>
| here is some server code<br>
| <snip><br>
| sock1.bind(('', port))<br>
| print "Listening at port: ", port<br>
| sock1.listen(1) # listening socket<br>
| (conn, addr) = sock1.accept() # connected socket<br>
| print 'Client (localhost) port: ', addr[1]<br>
|<br>
| cf = conn.makefile('r',0) # file like obj for socket<br>
</div>[...]<br>
<div class="im">| print 'close'<br>
| cf.flush()<br>
| cf.close()<br>
| sfp.close()<br>
</div>[...]<br>
<br>
Too many files. It's not that hard! Or shouldn't be.<br>
<div class="im"><br>
| So what I am wondering is:<br>
|<br>
| 1. Using a file-like object means that the socket becomes uni-<br>
| directional, until the mode of the file object is changed from 'r' to<br>
| 'w' (or vice versa). This seems inefficient, and rather unPythonesque.<br>
| Can somebody tell me if there is a more elegant way of receiving all<br>
| the lines from the client, and then sending a "done" message to the<br>
| client?<br>
<br>
</div>Get the socket. It is a file descriptor (or in may be a python "socket"<br>
object with a file descriptor inside).<br>
<br>
Open _two_ "file" objects on it using<br>
from_file = os.fdopen(fd_of_socket, "r")<br>
to_file = os.fdopen(fd_of_socket, "w").<br>
<br>
Use the same:<br>
print >>to_file, 'close'<br>
to_file.flush()<br>
method as you're using already.<br>
Read from to_file as needed.<br>
<br>
The same scheme should work in both server and client:<br>
<br>
Don't look for EOF, watch the input line flow.<br>
<br>
You might need to use readline() instead of the file-by-line iteration stuff,<br>
which I seem to recall has some sort of problem handing out the "latest"<br>
line.<br>
<br>
Cheers,<br>
<font color="#888888">--<br>
Cameron Simpson <<a href="mailto:cs@zip.com.au">cs@zip.com.au</a>> DoD#743<br>
<a href="http://www.cskk.ezoshosting.com/cs/" target="_blank">http://www.cskk.ezoshosting.com/cs/</a><br>
<br>
It's better, when you're riding with someone you don't know so well, to stick<br>
to the inside line - it's easier to avoid the bits...<br>
- Barry Sheene<br>
</font></blockquote></div><br>