views:

451

answers:

3

I'm writing an mini FTP server in Python that exposes an underlying database as if it was FTP. The flow is something like this:

sock.send("150 Here's the file you wanted\r\n")
proc = Popen2(...)
for parts in data:
    data_sock.send(parts)
proc.kill()
sock.send("226 There's the file you wanted\r\n")
data_sock.shutdown(0)
data_sock.close()

data_sock is the PASV socket that's up and working, confirmed by Wireshark. What's actually happening is after the 163,328th byte has been sent over the data_sock, the data_sock.send() line just hangs. I suspect the send buffer is full, but it's a mystery to me why the FTP clients wouldn't be reading from the PASV socket.

I've included the Popen2(...) line because I've managed to reproduce http://bugs.python.org/issue3006 on OS X--sockets don't close until the Popen process is killed. Not sure if this is somehow related.

A: 

I've encountered similar issues on the client side on uploads, which seem to trace to the modem/router choking -- the only workround I have at the moment is to throttle the transmission rate (send 128 bytes, sleep ~50ms, repeat).

Steve Gilham
I'm testing with both ends on localhost. Maybe this is wrong, but my mental model says that with nice priorities being equal, send/receive should be about the same speed, no?
Drew
+1  A: 

Hard to say from this code fragment and not knowing the client, but is it possible that your sending of 150 (indicating a new data channel), not 125 (indicating use of existing data channel) confuses the client and it simply does not start reading the data?

Have you had a look of pyftpdlib as an alternative for rolling your own server?

Marko Teiste
A: 
Jeremy Friesner