Python實(shí)現(xiàn)的tcp端口檢測(cè)操作示例
本文實(shí)例講述了Python實(shí)現(xiàn)的tcp端口檢測(cè)操作。分享給大家供大家參考,具體如下:
# coding=utf-8
import sys
import socket
import re
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 as e:
print 'Connection to %s on port %s failed: %s' % (address, port, e)
return False
if __name__ == '__main__':
from argparse import ArgumentParser
parser = ArgumentParser(description=u'TCP端口檢測(cè)')
parser.add_argument(
'-a',
'--address',
dest='address',
default='localhost',
help='address for the server')
parser.add_argument(
'-p',
'--port',
dest="port",
default=80,
type=int,
help='port for the server')
args = parser.parse_args()
check = check_server(args.address, args.port)
print 'check_server returned %s' % check
sys.exit(not check)
測(cè)試結(jié)果:
[hupeng@hupeng-vm Python]$python check_server.py && echo "SUCCESS"
Attempting to connect to localhost on port 80
Connected to localhost on port 80
check_server returned True
SUCCESS
[hupeng@hupeng-vm Python]$python check_server.py -p 81 && echo "Failure"
Attempting to connect to localhost on port 81
Connection to localhost on port 81 failed: [Errno 111] Connection refused
check_server returned False
[hupeng@hupeng-vm Python]$python check_server.py -p 81 || echo "Failure"
Attempting to connect to localhost on port 81
Connection to localhost on port 81 failed: [Errno 111] Connection refused
check_server returned False
Failure
附:
shell中&&和||的使用方法
命令的返回結(jié)果:真(返回0),假(返回非0)
command1 && command2: command1返回真時(shí),command2才會(huì)被執(zhí)行
command1 || command2:command1返回真時(shí),command2就不會(huì)被執(zhí)行
更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》
希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
python 實(shí)現(xiàn)提取log文件中的關(guān)鍵句子,并進(jìn)行統(tǒng)計(jì)分析
今天小編就為大家分享一篇python 實(shí)現(xiàn)提取log文件中的關(guān)鍵句子,并進(jìn)行統(tǒng)計(jì)分析,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python下載商品數(shù)據(jù)并連接數(shù)據(jù)庫(kù)且保存數(shù)據(jù)
這篇文章主要介紹了Python下載商品數(shù)據(jù)并連接數(shù)據(jù)庫(kù)且保存數(shù)據(jù),包括發(fā)送請(qǐng)求、獲取數(shù)據(jù)、解析數(shù)據(jù)(篩選數(shù)據(jù))、保存數(shù)據(jù)、連接數(shù)據(jù)庫(kù)等內(nèi)容,需要的小伙伴可以參考一下2022-03-03
Pycharm學(xué)生免費(fèi)專業(yè)版安裝教程的方法步驟
這篇文章主要介紹了Pycharm學(xué)生免費(fèi)專業(yè)版安裝教程的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-09-09
在python中利用try..except來(lái)代替if..else的用法
今天小編就為大家分享一篇在python中利用try..except來(lái)代替if..else的用法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-12-12
Python 模擬員工信息數(shù)據(jù)庫(kù)操作的實(shí)例
下面小編就為大家?guī)?lái)一篇Python 模擬員工信息數(shù)據(jù)庫(kù)操作的實(shí)例。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-10-10
citespace數(shù)據(jù)處理:用python對(duì)Ref文檔進(jìn)行去重方式
這篇文章主要介紹了citespace數(shù)據(jù)處理:用python對(duì)Ref文檔進(jìn)行去重方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-11-11

