欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)控中的服務(wù)器的Socket。
-n或--numeric  直接使用IP地址,而不通過域名服務(wù)器。
-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 使用裝飾器并記錄log的示例代碼

    python 使用裝飾器并記錄log的示例代碼

    今天小編就為大家分享一篇python 使用裝飾器并記錄log的示例代碼,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-07-07
  • Django操作session 的方法

    Django操作session 的方法

    這篇文章主要介紹了Django操作session 的方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2020-03-03
  • Pytorch?和?Tensorflow?v1?兼容的環(huán)境搭建方法

    Pytorch?和?Tensorflow?v1?兼容的環(huán)境搭建方法

    這篇文章主要介紹了搭建Pytorch?和?Tensorflow?v1?兼容的環(huán)境,本文是小編經(jīng)過多次實踐得到的環(huán)境配置教程,給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2022-11-11
  • Python知識點詳解之正則表達式語法

    Python知識點詳解之正則表達式語法

    正則表達式在搜索大型文本、電子郵件和文檔時非常有用,正則表達式也稱為"用于字符串匹配的編程語言",下面這篇文章主要給大家介紹了關(guān)于Python知識點之正則表達式語法的相關(guān)資料,需要的朋友可以參考下
    2022-05-05
  • Python實現(xiàn)拓撲算法的示例

    Python實現(xiàn)拓撲算法的示例

    本文主要介紹了Python實現(xiàn)拓撲算法的示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-05-05
  • Python基于identicon庫創(chuàng)建類似Github上用的頭像功能

    Python基于identicon庫創(chuàng)建類似Github上用的頭像功能

    這篇文章主要介紹了Python基于identicon庫創(chuàng)建類似Github上用的頭像功能,結(jié)合具體實例形式分析了identicon庫操作圖形的具體步驟與相關(guān)使用技巧,需要的朋友可以參考下
    2017-09-09
  • 在Python程序中實現(xiàn)分布式進程的教程

    在Python程序中實現(xiàn)分布式進程的教程

    這篇文章主要介紹了在Python程序中實現(xiàn)分布式進程的教程,在多進程編程中十分有用,示例代碼基于Python2.x版本,需要的朋友可以參考下
    2015-04-04
  • python用fsolve、leastsq對非線性方程組求解

    python用fsolve、leastsq對非線性方程組求解

    這篇文章主要為大家詳細介紹了python用fsolve、leastsq對非線性方程組進行求解,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2018-12-12
  • Python3爬蟲學習之將爬取的信息保存到本地的方法詳解

    Python3爬蟲學習之將爬取的信息保存到本地的方法詳解

    這篇文章主要介紹了Python3爬蟲學習之將爬取的信息保存到本地的方法,結(jié)合實例形式詳細分析了Python3信息爬取、文件讀寫、圖片存儲等相關(guān)操作技巧,需要的朋友可以參考下
    2018-12-12
  • 對Python3之方法的覆蓋與super函數(shù)詳解

    對Python3之方法的覆蓋與super函數(shù)詳解

    今天小編就為大家分享一篇對Python3之方法的覆蓋與super函數(shù)詳解,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-06-06

最新評論