I am trying to write a simple FTP server in order to learn about
sockets
This is my first time trying sockets

This code should take a connection on port 8110, dump it to a client
"thread" (not a thread yet), print "Test\n" to the thread, and close
everything.
It fails on creating the client thread with error  106: 'Transport
endpoint is already connected'

On running the code:
dftp starting
getting the socket at 127.0.0.1 at port 8110
listening for 5 connections
entering main loop
####pause here until telnet####
found a client @ <socket._socketobject object at 0x2ba16f6cd650> addr:
('127.0.0.1', 58643)
Creating a new client socket
Trying to connect
Traceback (most recent call last):
  File "./dftpd.py", line 46, in ?
    main()
  File "./dftpd.py", line 41, in main
    clientThread = Connection(clientsocket, address);
  File "/home/palmer/prog/dftp/dftpd/connection.py", line 29, in
__init__
    self.sock.connect(a);
  File "<string>", line 1, in connect
socket.error: (106, 'Transport endpoint is already connected')

Telnet output:
$ telnet localhost 8110
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
Connection closed by foreign host.

Here's my code:
config is a stand-in for a config-file reader, now it just runs
localhost:8110 and 5 connections
debug just prints to standard out (the number afterwords is the level,
but now everything is printed)
#################dftpd.py#########################
import debug
import socket
import config
from connection import Connection

shouldRun=True;

def main():
        debug.stdout("dftp starting", 1)
        debug.stdout("getting the socket at " + str(config.listenAddress()) +
" at port " + str(config.listenPort()), 10);
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM);
        sock.bind((config.listenAddress(), config.listenPort()));
        debug.stdout("listening for " + str(config.listenConnections()) + "
connections", 10);
        sock.listen(config.listenConnections());
        debug.stdout("entering main loop", 10);
        while shouldRun:
                (clientsocket, address) = sock.accept();
                debug.stdout("found a client @ " + str(clientsocket) + " addr: 
" +
str(address), 10);
                clientThread = Connection(clientsocket, address);
                print clientThread.write("Test\n");
                clientThread.close;
        sock.close();

main()

########################connection.py########################
import debug;
import socket;

class Connection:
        def __init__(self, s, a):
                debug.stdout("Creating a new client socket", 15);
                self.sock = s;
                debug.stdout("Trying to connect", 15);
                self.sock.connect(a);
                debug.stdout("Making the file over the socket", 15);
                self.file = self.sock.makefile();
        def read():
                return file.read();
        def write(out):
                file.write(out);
        def close():
                file.close();
                sock.close();

-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to