最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Python找出9個連續(xù)的空閑端口

 更新時間:2016年02月01日 14:57:33   作者:學習編程知識  
這篇文章主要介紹了Python找出9個連續(xù)的空閑端口的方法,感興趣的小伙伴們可以參考一下

一、項目需求

安裝某軟件,配置時候需要填寫空閑的端口。查看5個平臺的某個端口是否被占用

5個平臺為windows, linux, aix, hp, solaris

二、實現(xiàn)方案有兩種

1、利用 python 的 socket 模塊里的

def isInuse(ipList, port):
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  flag=True
  for ip in ipList:
    try:
      s.connect((ip, int(port)))
      s.shutdown(2)
      print '%d is inuse' % port
      flag=True
      break
    except:
      print '%d is free' % port
      flag=False
  return flag

在try 模塊中 如果 s.connect((ip, int(port))) 如果能成功, 說明端口被占用.

否則, connect 不成功, 會進到except 中, 說明端口不被占用.

但是有個問題, 端口監(jiān)聽的ip 除了 "127.0.0.1","0.0.0.0" 還有可能是本機的局域網(wǎng)ip 如 222.25.136.17 , 或者與之通信的那臺機器的ip。

可以通過這個方法獲得局域網(wǎng) ip

def getLocalIp():
  localIP = socket.gethostbyname(socket.gethostname())
  return localIP

本代碼只針對 ipList = ("127.0.0.1","0.0.0.0",getLocalIp()) 這3個 ip 進行 connect

import sys
import os
import socket


def isInuse(ipList, port):
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  flag=True
  for ip in ipList:
    try:
      s.connect((ip, int(port)))
      s.shutdown(2)
      print '%d is inuse' % port
      flag=True
      break
    except:
      print '%d is free' % port
      flag=False
  return flag


def getLocalIp():
  localIP = socket.gethostbyname(socket.gethostname())
  return localIP

def checkNinePort(startPort):
  flag = True
  ipList = ("127.0.0.1","0.0.0.0",getLocalIp())
  for i in range(1, 10):
    if (isInuse(ipList, startPort)):
      flag = False
      break
    else:
      startPort = startPort + 1
  return flag, startPort


def findPort(startPort):
  while True:
    flag, endPort = checkNinePort(startPort)
    if (flag == True): #ninePort is ok
      break
    else:
      startPort = endPort + 1
  return startPort


def main():
  startPort=51988
  # startPort = int(sys.argv[1])
  print findPort(startPort)

main()

2. 利用netstat 輸出信息查找端口號匹配

第一種方法的準確性依賴于 connect((ip, int(port))) 中的 ip,到底怎樣的 ip 集合才是完備的, 可以確定這個端口不被占用?

于是, 有下面這個方法

**在 linux 用 netstat -tnpl 可以得到端口監(jiān)聽信息,

觀察 tcp 0 0 10.173.1.208:3210 0.0.0.0:* LISTEN 55563/repserver

出現(xiàn)了 10.173.1.208:3210 所以 3210 端口是被占用的

對這些信息進行搜索 :5000, 如果存在, 就表示5000端口是LISTEN**.

如果輸出結(jié)果中不存在 :5000 的相關(guān)字符,表示這個端口不被占用.

netstat - tnpl | grep 321

tcp 0 0 10.173.1.208:3211 0.0.0.0:* LISTEN 55563/***
tcp 0 0 0.0.0.0:3212 0.0.0.0:* LISTEN 55586/***
tcp 0 0 10.173.1.208:3213 0.0.0.0:* LISTEN 55707/***
tcp 0 0 0.0.0.0:3214 0.0.0.0:* LISTEN 54272/java
tcp 0 0 0.0.0.0:3215 0.0.0.0:* LISTEN 54272/java
tcp 0 0 10.173.1.208:3216 0.0.0.0:* LISTEN 54822/***
tcp 0 0 10.173.1.208:3217 0.0.0.0:* LISTEN 34959/***
tcp 0 0 10.173.1.208:3218 0.0.0.0:* LISTEN 54849/***

依據(jù)這個思路, 給出代碼.

AIX 、HP 、WINDOWS、 LINUX、 SOLARIS 這幾個平臺查看端口信息的方式不同,

先進行機器平臺的判斷

然后調(diào)用各個平臺的端口占用判斷函數(shù)

如果要找出連續(xù)端口, 其中只要有一個端口占用, 就跳出循環(huán)

__author__ = 'I316736'
import os
import platform
import sys


def isInuseWindow(port):
  if os.popen('netstat -an | findstr :' + str(port)).readlines():
    portIsUse = True
    print '%d is inuse' % port
  else:
    portIsUse = False
    print '%d is free' % port
  return portIsUse

def isInuseLinux(port):
  #lsof -i:4906
  #not show pid to avoid complex
  if os.popen('netstat -na | grep :' + str(port)).readlines():
    portIsUse = True
    print '%d is inuse' % port
  else:
    portIsUse = False
    print '%d is free' % port
  return portIsUse

def isInuseAix(port):
  if os.popen('netstat -Aan | grep "\.' + str(port) + ' "').readlines():
    portIsUse = True
    print '%d is inuse' % port
  else:
    portIsUse = False
    print '%d is free' % port
  return portIsUse

def isInuseHp(port):
  if os.popen('netstat -an | grep "\.' + str(port) + ' "').readlines():
    portIsUse = True
    print '%d is inuse' % port
  else:
    portIsUse = False
    print '%d is free' % port
  return portIsUse

def isInuseSun(port):
  if os.popen('netstat -an | grep "\.' + str(port) + ' "').readlines():
    portIsUse = True
    print '%d is inuse' % port
  else:
    portIsUse = False
    print '%d is free' % port
  return portIsUse

def choosePlatform():
  #'Windows-7-6.1.7601-SP1'
  #'AIX-1-00F739CE4C00-powerpc-32bit'
  #'HP-UX-B.11.31-ia64-32bit'
  #'Linux-3.0.101-0.35-default-x86_64-with-SuSE-11-x86_64'
  #'SunOS-5.10-sun4u-sparc-32bit-ELF'
  machine = platform.platform().lower()
  if 'windows-' in machine:
    return isInuseWindow
  elif 'linux-' in machine:
    return isInuseLinux
  elif 'aix-' in machine:
    return isInuseAix
  elif 'hp-' in machine:
    return isInuseHp
  elif 'sunos-' in machine:
    return isInuseSun
  else:
    print 'Error, sorry, platform is unknown'
    exit(-1)

def checkNinePort(startPort):
  isInuseFun = choosePlatform()
  nineIsFree = True
  for i in range(1, 10):
    if (isInuseFun(startPort)):
      nineIsFree = False
      break
    else:
      startPort = startPort + 1
  return nineIsFree, startPort


def findPort(startPort):
  while True:
    flag, endPort = checkNinePort(startPort)
    if (flag == True): # ninePort is ok
      break
    else:
      startPort = endPort + 1
  return startPort


def main(startPort):
  firstPort=findPort(startPort)
  print 'First port of nine free ports is ', firstPort

if __name__ == '__main__' :
  if len(sys.argv) > 1:
    print len(sys.argv)
    startPort = int(sys.argv[1])
  else:
    startPort = 500
  main(startPort)

相關(guān)知識點總結(jié)

os.popen()
可以調(diào)用系統(tǒng)的一些shell命令

os.popen().readlines()
讀取調(diào)用shell命令后的回顯信息

 netstat -tnpl 

-tnpl 各個參數(shù)的含義
-l或--listening  顯示監(jiān)控中的服務器的Socket。
-n或--numeric  直接使用IP地址,而不通過域名服務器。
-p或--programs  顯示正在使用Socket的程序識別碼和程序名稱。
-t或--tcp  顯示TCP傳輸協(xié)議的連線狀況

----------

tcp 0 0 10.173.1.208:4903 0.0.0.0:* LISTEN 54849/jsagent
最后的54849/jsagent 表示 進程號 54849 進程名 jsagent

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助。

相關(guān)文章

  • python 巡檢腳本的項目實踐

    python 巡檢腳本的項目實踐

    本文主要介紹了python 巡檢腳本的項目實踐,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • Python 實現(xiàn)Windows開機運行某軟件的方法

    Python 實現(xiàn)Windows開機運行某軟件的方法

    今天小編就為大家分享一篇Python 實現(xiàn)Windows開機運行某軟件的方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • python報錯unexpected?indent的解決辦法

    python報錯unexpected?indent的解決辦法

    這篇文章主要給大家介紹了關(guān)于python報錯unexpected?indent的解決辦法,在python中出現(xiàn)"Unexpected indent"可能是代碼的縮進出現(xiàn)問題,需要的朋友可以參考下
    2023-06-06
  • 使用Ray集群簡單創(chuàng)建Python分布式應用程序

    使用Ray集群簡單創(chuàng)建Python分布式應用程序

    面對計算密集型的任務,除了多進程,就是分布式計算,如何用 Python 實現(xiàn)分布式計算呢?今天分享一個很簡單的方法,那就是借助于 Ray
    2021-09-09
  • Python實現(xiàn)提取給定網(wǎng)頁內(nèi)的所有鏈接

    Python實現(xiàn)提取給定網(wǎng)頁內(nèi)的所有鏈接

    這篇文章主要和大家分享一個實用的Python腳本,可以實現(xiàn)從給定的網(wǎng)頁中檢索所有鏈接,并將其保存為txt文件,需要的小伙伴可以收藏一下
    2023-05-05
  • 利用OpenCV判斷是否加載圖片的兩種方法

    利用OpenCV判斷是否加載圖片的兩種方法

    這篇文章主要介紹了利用OpenCV判斷是否加載圖片的兩種方法,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • 在Python3.74+PyCharm2020.1 x64中安裝使用Kivy的詳細教程

    在Python3.74+PyCharm2020.1 x64中安裝使用Kivy的詳細教程

    這篇文章主要介紹了在Python3.74+PyCharm2020.1 x64中安裝使用Kivy的詳細教程,本文通過圖文實例相結(jié)合給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-08-08
  • Python+wxPython實現(xiàn)自動生成PPTX文檔程序

    Python+wxPython實現(xiàn)自動生成PPTX文檔程序

    這篇文章主要介紹了如何使用 wxPython 模塊和 python-pptx 模塊來編寫一個程序,用于生成包含首頁、內(nèi)容頁和感謝頁的 PPTX 文檔,感興趣的小伙伴可以學習一下
    2023-08-08
  • python讀取npy文件數(shù)據(jù)實例

    python讀取npy文件數(shù)據(jù)實例

    npy文件用于存儲重建ndarray所需的數(shù)據(jù)、圖形、dtype?和其他信息,下面這篇文章主要給大家介紹了關(guān)于python讀取npy文件數(shù)據(jù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,需要的朋友可以參考下
    2022-04-04
  • Python掃描IP段查看指定端口是否開放的方法

    Python掃描IP段查看指定端口是否開放的方法

    這篇文章主要介紹了Python掃描IP段查看指定端口是否開放的方法,涉及Python使用socket模塊實現(xiàn)端口掃描功能的相關(guān)技巧,需要的朋友可以參考下
    2015-06-06

最新評論

华阴市| 永登县| 道真| 平远县| 溧阳市| 刚察县| 论坛| 英山县| 湖南省| 宿松县| 永康市| 丹东市| 花垣县| 化州市| 鹤壁市| 洪湖市| 白山市| 昂仁县| 厦门市| 棋牌| 黔西县| 铁岭县| 金沙县| 莱芜市| 广饶县| 黔西| 滦平县| 精河县| 鄂州市| 奎屯市| 临沭县| 平湖市| 敖汉旗| 苏州市| 长丰县| 新河县| 达拉特旗| 庄浪县| 长宁区| 正蓝旗| 宝清县|