Python的socket模塊為操作系統的socket實現提供了一個python接口。
In [168]: import socket
In [169]: s=socket.socket()
In [170]: s.connect(("127.0.0.1",80))
In [171]: s.send("GET / HTTP/1.1\n\n")
Out[171]: 16
In [172]: s.recv(200)
Out[172]: 'HTTP/1.1 400 Bad Request\r\nServer: nginx\r\nDate: Mon, 21 Jul 2014 02:34:14 GMT\r\nContent-Type: text/html\r\nContent-Length: 166\r\nConnection: close\r\n\r\n<html>\r\n<head><title>400 Bad Request</title></head>\r\n<b'
In [173]: s.close()
先創建一個名為s的socket對象,然後連接到本地的Nginx服務器,這裡要注意一下connect只能帶一個參數,如果是IP和端口對需要用括號括住。然後通過send()函數發送一個HTTP請求,然後接收服務端響應的前200個字節。最後關閉socket連接
#/usr/bin/python
import socket
import re
import sys
def check_server(address, port):
s=socket.socket()
print "Attempting to connect to %s on port %s" %(address,port)
try:
s.connect((address,port))
print "Connected to %s on port %s" %(address,port)
return True
except socket.error,e:
print "Connection to %s on port %s failed: %s" %(address,port,e)
return False
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-a", "--address", dest="address",default='localhost', help="Address for the server", metavar="ADDRESS")
parser.add_option("-p", "--port", dest="port",type="int", default=80,help="PORT for server",metavar="PORT")
(options, args) = parser.parse_args()
print 'options: %s,args: %s' %(options,args)
check=check_server(options.address,options.port)
print 'check server returned %s' %check
sys.exit(not check)
以上程序用於探測遠程主機的端口狀態,通過socket的connect()函數去嘗試連接遠程主機的給定端口。後面主要用到optparse模塊添加選項。
$ python tcp_port_checker.py -a 10.10.41.20 -p 80
options: {'port': 80, 'address': '10.10.41.20'},args: []
Attempting to connect to 10.10.41.20 on port 80
Connected to 10.10.41.20 on port 80
check server returned True
$ python tcp_port_checker.py -a 10.10.41.20 -p 80 && echo "SUCCESS"
options: {'port': 80, 'address': '10.10.41.20'},args: []
Attempting to connect to 10.10.41.20 on port 80
Connected to 10.10.41.20 on port 80
check server returned True
SUCCESS
$ python tcp_port_checker.py -a 10.10.41.20 -p 81 || echo "FAILURE"
options: {'port': 81, 'address': '10.10.41.20'},args: []
Attempting to connect to 10.10.41.20 on port 81
Connection to 10.10.41.20 on port 81 failed: [Errno 111] Connection refused
check server returned False
FAILURE
一個web server的80端口開放並不能代表這台服務器上就運行著web服務,還需要通過HTTP響應返回值來判斷。
#/usr/bin/python
import socket
import re
import sys
def check_webserver(address,port,resource):
if not resource.startswith('/'):
resource = '/' + resource
request_string = "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n" %(resource,address)
print 'HTTP request:'
print '|||%s|||' %request_string
s=socket.socket()
print "Attempting to connect to %s on port %s" %(address,port)
try:
s.connect((address,port))
print "Connected to %s on port %s" %(address,port)
s.send(request_string)
rsp=s.recv(100)
print "Received 100 bytes of HTTP response"
print '|||%s|||' %rsp
except socket.error,e:
print "Connection to %s on port %s failed: %s" %(address,port,e)
print "Closing the connection"
s.close()
lines=rsp.splitlines()
print "First line of HTTP response: %s" %lines[0]
try:
version,status,message=re.split(r'\s+',lines[0],2)
print 'Version: %s, Status: %s, Message: %s' %(version,status,message)
except ValueError:
print 'Failed to split status line'
return False
if status in ['200','301']:
print 'Success - status was %s' %status
return True
else:
print 'Status was %s' %status
return False
if __name__ == '__main__':
from optparse import OptionParser
parser = OptionParser()
parser.add_option("-a","--address",dest="address",default='localhost',help="ADDRESS for webserver",metavar="ADDRESS")
parser.add_option("-p","--port",dest="port",type="int",default=80,help="PORT for webserver",metavar="PORT")
parser.add_option("-r","--resource",dest="resource",default='index.html',help="RESOURCE for webserver",metavar="RESOURCE")
(options,args)=parser.parse_args()
print 'options: %s,args: %s' %(options,args)
check=check_webserver(options.address,options.port,options.resource)
print 'check_webserver returned %s' %check
sys.exit(not check)
以上程序主要通過check_webserver函數去探測HTTP服務,檢測端口是否可以連接,並發送HTTP請求,通過web server的返回值進行判斷。
$ python tcp_port_checker2.py -a 10.10.41.20 -p 80 -r index.php
options: {'resource': 'index.php', 'port': 80, 'address': '10.10.41.20'},args: []
HTTP request:
|||GET /index.php HTTP/1.1
Host: 10.10.41.20
|||
Attempting to connect to 10.10.41.20 on port 80
Connected to 10.10.41.20 on port 80
Received 100 bytes of HTTP response
|||HTTP/1.1 404 Not Found
Server: nginx
Date: Tue, 22 Jul 2014 03:24:17 GMT
|||tent-Type: text/html
Closing the connection
First line of HTTP response: HTTP/1.1 404 Not Found
Version: HTTP/1.1, Status: 404, Message: Not Found
Status was 404
check_webserver returned False
《Python開發技術詳解》.( 周偉,宗傑).[高清PDF掃描版+隨書視頻+代碼] http://www.linuxidc.com/Linux/2013-11/92693.htm
Python腳本獲取Linux系統信息 http://www.linuxidc.com/Linux/2013-08/88531.htm
Python下使用MySQLdb模塊 http://www.linuxidc.com/Linux/2012-06/63620.htm
Python 的詳細介紹:請點這裡
Python 的下載地址:請點這裡