今天看到python 的网络编程,顺便写个小程序实践一下。
脚本功能:
运用python 程序,向服务器发送socket 请求,根据请求状态实现对linux 主机的监控。
程序代码:
#!/usr/bin/python2.6
import socket
import re
import sys
import MySQLdb
def check_server(address,port):
s = socket.socket()
try:
s.connect((address,port))
print "connected to %s on port %s " % (address,port)
return True
except socket.error:
print "connected failed to %s on port %s " % (address,port)
return False
#check_server(sys.argv[1],int(sys.argv[2]))
#check_server(ip,port)
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-a","--address",dest = "address", default = 'localhost',help =