开发者

Network - testing connectivity [Python or C]

开发者 https://www.devze.com 2023-01-05 01:11 出处:网络
Let\'s sa开发者_Python百科y I want to see if my ftp server is online, how could I do this in a program.

Let's sa开发者_Python百科y I want to see if my ftp server is online, how could I do this in a program. Also, what do you think would be the easiest least intrusive way.


Personally, I would try nmap first to do this, http://nmap.org.

nmap $HOSTNAME -p 21

To test port 21 (ftp) on a list of servers in python might look like this:

#!/usr/bin/env python  
from socket import *   

host_list=['localhost', 'stackoverflow.com']

port=21 # (FTP port)

def test_port(ip_address, port, timeout=3):
    s = socket(AF_INET, SOCK_STREAM)
    s.settimeout(timeout)
    result = s.connect_ex((ip_address, port))
    s.close()
    if(result == 0):
        return True
    else:
        return False

for host in host_list:
    if test_port(gethostbyname(host), port):
        print 'Successfully connected to',
    else:
        print 'Failed to connect to',
    print '%s on port %d' % (host, port)


Connect to the port of your FTP server to see if it is accepting connections.

If you want to go one step further you could send an ls command and check that you get a sensible response.

If you want to do it in Python you can use ftplib.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号