سؤال

I am trying to write a piece of python code which listens on two different ports. In Java I could do this simply by launching a thread which listened on each of the port but it does seem like it's working that way in Python. The code I have so far is:

from socket import *
import thread
from datetime import datetime

BUFF_SIZE = 1024
HOST = '' # listen on all interfaces
PORT_FTP = 5000 # port to listen on
PORT_ESMTP = 5001 # TODO: change to real port

# HEADERS #################################
headerFtpGuild = "220-GuildFTPd FTP Server (c) 1997-2002\r\n220-Version 0.999.14\r\n"
headerFtpCerberus = "220-Cerberus FTP Server Personal Edition\r\n220-UNREGISTERED\r\n";
headerESMTP = "220 ESMTP service ready\r\n250\x20ok\r\n";
###########################################

def handlerFTP(clientsock,addr):
    try:
        clientsock.send(headerFtpGuild)
        print '[*] Faked FTP banner'
        clientsock.close()
    except:
        pass

def handlerESMTP(clientsock,addr):
    try:
        clientsock.send(headerESMTP)
        print '[*] Faked ESMTP banner'
        clientsock.close()
    except:
        pass

def getTime():
    return datetime.now().strftime('%Y-%m-%d %H:%M:%S')


def listenFTP():
    ADDR = (HOST, PORT_FTP)
    serversock = socket(AF_INET, SOCK_STREAM)
    serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    serversock.bind(ADDR)
    serversock.listen(1)

    print '[*]FTP'

    while 1:
        clientsock, addr = serversock.accept()
        print '[ ] possible probe detected from:', addr[0], '@', getTime()
        thread.start_new_thread(handlerFTP, (clientsock, addr))

def listenESMTP():
    ADDR = (HOST, PORT_ESMTP)
    serversock1 = socket(AF_INET, SOCK_STREAM)
    serversock1.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
    serversock1.bind(ADDR)
    serversock1.listen(1)

    print '[*]ESMTP'

    while 1:
        clientsock1, addr1 = serversock1.accept()
        print '[ ] possible probe detected from:', addr1[0], '@', getTime()
        thread.start_new_thread(handlerESMTP, (clientsock1, addr1))

if __name__=='__main__':
    listenFTP()
    listenESMTP()

It seems only listenFTP() is being excecuted. What can I do about this? Thanks in advance.

هل كانت مفيدة؟

المحلول

I think basically you're not using threading in your python's code. You fire up the FTP listener and of course, this method will execute forever. You can add a quickfix with thread.start_new_thread function like this:

if __name__=='__main__':
    import thread
    thread.start_new_thread(listenFTP)
    thread.start_new_thread(listenESMTP)

Hope this helps!

نصائح أخرى

Try putting listenFTP() and listenESMTP() in an infinite loop or while loop.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top