Python實(shí)現(xiàn)的tcp端口檢測操作示例
本文實(shí)例講述了Python實(shí)現(xiàn)的tcp端口檢測操作。分享給大家供大家參考,具體如下:
# 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端口檢測') 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)
測試結(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文件與目錄操作技巧匯總》
希望本文所述對大家Python程序設(shè)計(jì)有所幫助。
相關(guān)文章
10分鐘教你用Python實(shí)現(xiàn)微信自動(dòng)回復(fù)功能
今天,我們就來用Python實(shí)現(xiàn)微信的自動(dòng)回復(fù)功能吧,并且把接收到的消息統(tǒng)一發(fā)送到文件助手里面,方便統(tǒng)一查看。感興趣的朋友跟隨小編一起看看吧2018-11-11配置python連接oracle讀取excel數(shù)據(jù)寫入數(shù)據(jù)庫的操作流程
這篇文章主要介紹了配置python連接oracle,讀取excel數(shù)據(jù)寫入數(shù)據(jù)庫,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03Python實(shí)現(xiàn)將MySQL數(shù)據(jù)庫表中的數(shù)據(jù)導(dǎo)出生成csv格式文件的方法
這篇文章主要介紹了Python實(shí)現(xiàn)將MySQL數(shù)據(jù)庫表中的數(shù)據(jù)導(dǎo)出生成csv格式文件的方法,涉及Python針對mysql數(shù)據(jù)庫的連接、查詢、csv格式數(shù)據(jù)文件的生成等相關(guān)操作技巧,需要的朋友可以參考下2018-01-01pygame實(shí)現(xiàn)井字棋之第二步邏輯實(shí)現(xiàn)
這篇文章主要介紹了pygame實(shí)現(xiàn)井字棋之第二步邏輯實(shí)現(xiàn),文中有非常詳細(xì)的代碼示例,對正在學(xué)習(xí)python的小伙伴們有非常好的幫助,需要的朋友可以參考下2021-05-05Flask中基于Token的身份認(rèn)證的實(shí)現(xiàn)
本文主要介紹了Flask中基于Token的身份認(rèn)證的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-02-02Pycharm編輯器技巧之自動(dòng)導(dǎo)入模塊詳解
我們在編程過程中經(jīng)常會(huì)不經(jīng)意的使用到一些尚未導(dǎo)入的類和模塊,在這種情況下Pycharm會(huì)幫助我們定位模塊文件位置并將其添加到導(dǎo)入列表中,這也就是所謂的自動(dòng)導(dǎo)入模塊功能。本文給大家介紹了關(guān)于Pycharm編輯器技巧之自動(dòng)導(dǎo)入模塊的相關(guān)資料,需要的朋友可以參考下。2017-07-07