分类: Python

Python3 Ping功能的若干种实现方式

方式一:自己编写实现ping功能

#!/usr/bin/env python3
# -*- encoding = utf-8 -*-
# 优点:全部通过python3自带库实现,无需安装任何第三方依赖库
# 缺点:需要root权限
# 用法:sudo python3 ping.py <host>

import time
import struct
import socket
import select
import sys

def chesksum(data):
  """
  校验
  """
  n = len(data)
  m = n % 2
  sum = 0 
  for i in range(0, n - m ,2):
    sum += (data[i]) + ((data[i+1]) << 8)#传入data以每两个字节(十六进制)通过ord转十进制,第一字节在低位,第二个字节在高位
  if m:
    sum += (data[-1])
  #将高于16位与低16位相加
  sum = (sum >> 16) + (sum & 0xffff)
  sum += (sum >> 16) #如果还有高于16位,将继续与低16位相加
  answer = ~sum & 0xffff
  #主机字节序转网络字节序列(参考小端序转大端序)
  answer = answer >> 8 | (answer << 8 & 0xff00)
  return answer 

  '''
  连接套接字,并将数据发送到套接字
  '''
def raw_socket(dst_addr,imcp_packet):
  rawsocket = socket.socket(socket.AF_INET,socket.SOCK_RAW,socket.getprotobyname("icmp"))
  send_request_ping_time = time.time()
  #send data to the socket
  rawsocket.sendto(imcp_packet,(dst_addr,80))
  return send_request_ping_time,rawsocket,dst_addr

  '''
  request ping
  '''
def request_ping(data_type,data_code,data_checksum,data_ID,data_Sequence,payload_body):
  #把字节打包成二进制数据
  imcp_packet = struct.pack('>BBHHH32s',data_type,data_code,data_checksum,data_ID,data_Sequence,payload_body)
  icmp_chesksum = chesksum(imcp_packet)#获取校验和
  imcp_packet = struct.pack('>BBHHH32s',data_type,data_code,icmp_chesksum,data_ID,data_Sequence,payload_body)
  return imcp_packet
  '''
  reply ping
  '''
def reply_ping(send_request_ping_time,rawsocket,data_Sequence,timeout = 2):
  while True:
    started_select = time.time()
    what_ready = select.select([rawsocket], [], [], timeout)
    wait_for_time = (time.time() - started_select)
    if what_ready[0] == []: # Timeout
      return -1
    time_received = time.time()
    received_packet, addr = rawsocket.recvfrom(1024)
    icmpHeader = received_packet[20:28]
    type, code, checksum, packet_id, sequence = struct.unpack(
      ">BBHHH", icmpHeader
    )
    if type == 0 and sequence == data_Sequence:
      return time_received - send_request_ping_time
    timeout = timeout - wait_for_time
    if timeout <= 0:
      return -1

  '''
  实现 ping 主机/ip
  '''
def ping(host):
  data_type = 8 # ICMP Echo Request
  data_code = 0 # must be zero
  data_checksum = 0 # "...with value 0 substituted for this field..."
  data_ID = 0 #Identifier
  data_Sequence = 1 #Sequence number
  payload_body = b'abcdefghijklmnopqrstuvwabcdefghi' #data
  dst_addr = socket.gethostbyname(host)#将主机名转ipv4地址格式,返回以ipv4地址格式的字符串,如果主机名称是ipv4地址,则它将保持不变
  print("正在 Ping {0} [{1}] 具有 32 字节的数据:".format(host,dst_addr))
  for i in range(0,4):
    icmp_packet = request_ping(data_type,data_code,data_checksum,data_ID,data_Sequence + i,payload_body)
    send_request_ping_time,rawsocket,addr = raw_socket(dst_addr,icmp_packet)
    times = reply_ping(send_request_ping_time,rawsocket,data_Sequence + i)
    if times > 0:
      print("来自 {0} 的回复: 字节=32 时间={1}ms".format(addr,int(times*1000)))
      time.sleep(0.7)
    else:
      print("请求超时。")

if __name__ == "__main__":
  if len(sys.argv) < 2:
    sys.exit('Usage: ping.py <host>')
  ping(sys.argv[1])

方式二:通过更为强大的scapy库实现

#!/usr/bin/env python3
# -*- encoding = utf-8 -*-
# sudo apt install python3-scapy
# 调用scapy库实现ping功能
# 优点:几乎没有
# 缺点:需要root权限
from scapy.layers.inet import IP, ICMP
from scapy.packet import Raw
from scapy.sendrecv import sr1
import logging
logging.getLogger("scapy.runtime").setLevel(logging.ERROR)  # 清除报错信息

def ping_one(dst):  # 构建函数
    ping_pkt = IP(dst=dst)/ICMP() / b'welcome!'  # 构建数据包,目的地址为dst,数据部分为字节字符串welcome!
    ping_result = sr1(ping_pkt, timeout=2, verbose=False)  # 发送数据包并将返回结构赋值到ping_result中
    try:
        if ping_result.getlayer(IP).fields['src'] == dst and ping_result.getlayer(ICMP).fields['type'] == 0 \
                and ping_result.getlayer(Raw).fields['load'] == b'welcome!':
            # 判断返回数据包是否为reply,通过源地址,返回数据与返回类型进行判断
            return dst, 1  # 如果为返回数据包,函数返回输入的目的地址与1
        else:
            return dst, 2  # 如果不是返回数据包,则函数2
    except Exception:  # 如果无法ping通,则不会有返回结果,会出现报错,如果产生报错,直接返回dst与2
        return dst, 2

if __name__ == '__main__':
    try:
        while True:
            dst = input('请输入目的IP地址:')
            if dst == '':
                print('目的地址为空,请重新输入!')
            else:
                break
        result = ping_one(dst)
        if result[-1] == 1:
            print('目的', result[0], '可达!')
        else:
            print('目的', result[0], '不可达!')
    except KeyboardInterrupt:
        print('成功接收信号,退户程序!')

方式三:使用tcping实现

#!/usr/bin/env python3
# -*- encoding = utf-8 -*-
# 优点:无需root权限
# 缺点:需要安装依赖tcping,仅能通过tcp实现ping,必须指定端口
# python3 -m pip install tcping
# https://pypi.org/project/tcping/
from tcping import Ping
# ping_check(地址,端口,超时秒数,ping次数)
def ping_check(address,port,timeout,times):
    ping = Ping(address, port, timeout)
    ping.ping(times) #ping 4 次
    statistics = ping.result.rows
    # statistics = [Statistics(host='192.168.1.1', port='80', successed=4, failed=0, success_rate='100.00%', minimum='0.09ms', maximum='0.25ms', average='0.14ms')]
    print(statistics[0])
    ret = ping.result.raw
    print(ret)
    ret = ping.result.table
    print(ret)
    #从返回的statistics元组中获得成功次数和平均延迟
    successed = statistics[0][2]
    # 返回状态及平均延迟
    if successed > 0:
        return True, statistics[0][-1]
    else:
        return False, statistics[0][-1]
if __name__ == '__main__':
    i = input("请输入要Ping的地址和端口(例:192.168.1.1:80):")
    addr = i.split(":")
    ping_result = ping_check(addr[0],addr[1],5,4)
    print(ping_result)

Python脚本:基于ffmpeg的视频格式转换工具

现在NAS已经很普及了,NAS上经常会存储海量的高清电影,但有时候我们希望把电影格式转换成便携一些的格式,直接在服务器上转换会更加的高效,因此我便写了这一个工具,可以做到无人值守的批量转换整个目录下任意视频。
只需要把这个脚本放在需要转换视频的目录下,然后运行即可。如果需要压制字幕,字幕文件名需要跟视频文件名保持完全一致(大小写敏感)。字幕支持srt和ass,utf8编码。如果在压制带字幕的视频时出现红字错误,先检查字幕编码是否正确,通过命令行cat查看字幕文件,如果输出乱码,则需要先对字幕文件进行转码处理。
支持以下几种创建转换队列的方式:

  1. 通过输入文件编号,用逗号或者空格或者点隔开,如1,2 3.4,会选择1、2、3、4号文件加入转换队列
  2. 通过输入文件区间,用英文减号隔开,如1-4,会选择1、2、3、4文件加入转换队列

每次转换,整个队列的转换参数是一致的。如果需要不同的转换参数,请分多次运行本脚本。
如果需要转换的队列太大,建议直接实机运行或者通过VNC运行,如果通过ssh运行本脚本,ssh超时断开会导致任务中断。转换后的视频在原视频目录下的out目录中,格式为mp4。转换时在out目录下遇到同名文件会直接覆盖,不提示。
注意:本脚本可以在Linux和Mac上运行,python3.7以上版本,需要提前安装好ffmpeg,最好是编译安装,with-libx264, with-lamemp3,压制ass格式的外挂字幕的话,还需要with-libass
话不多说,直接上代码:(文末附原文件下载)

#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import subprocess
import os,sys
#指定当前.py脚本的绝对路径
current_path = sys.path[0]
#指定ffmpeg的路径:
ffmpeg_path = "/usr/local/ffmpeg/bin/ffmpeg"
print("\033[36m","="*40,"\033[0m")
print("\033[35m欢迎使用ffmpeg视频转码脚本 v1.2\033[0m")
print("\033[35mLast Update: 2021/12/21\033[0m")
print("\033[35mAuthor: 1Fan Tech\033[0m")
print("\033[35m将本脚本与需要转换的视频放在同一目录下\033[0m")
print("\033[35m转换后的mp4视频输出在out目录\033[0m")
print("\033[35m如果目标视频已存在会自动覆盖\033[0m")
print("\033[36m","="*40,"\033[0m")
print("\033[32m当前目录文件列表:\033[0m")

#输出当前目录文件列表
print("\033[32m",current_path,"\033[0m")
#判断文件是否为视频文件
def is_video(t_file):
    v_list = ["mp4","mkv","mpg","mpeg","webm","flv","avi","mov"]
    rst = False
    for v in v_list:
        if t_file.lower().endswith(v):
            rst = True
    return rst
# x - y 类型字符串转换为列表
def dash_into_list(t_str):
    out_list = []
    if "-" in t_str:
        t_list = t_str.replace(" ","").split("-")
        try:
            t_list.remove("")
        except:
            pass
        if len(t_list)==2:
            try:
                start = int(t_list[0])
                end = int(t_list[1])
                if start<end:
                    for x in range(start,end+1):
                        out_list.append(x)
            except:
                pass
    return out_list
file_index = 0
file_list = []
print("\033[34m",file_index,".\t\033[32m  退出\033[0m")
for tmp_file in os.listdir(current_path):
    if not os.path.isdir(tmp_file) and is_video(tmp_file):
        real_file = os.path.join(current_path, tmp_file)
        file_list.append(real_file)
        file_index+=1
        file_size = int(os.path.getsize(real_file)/10485.76)/100 #文件大小以两位小数显示Mb
        print("\033[34m",file_index,".\t\033[32m ",tmp_file,"\033[33m\t",file_size,"MB\033[0m")
if file_index>0:
    loop_indicator = 0
    new_error = 0
    while loop_indicator == 0:
        new_error = 0
        print("\033[34m请输入您要转换的文件序号(1 - ",file_index,")可选择多个文件以逗号或空格或点分隔,或者通过x-y批量选择:\033[0m", end="")
        nums = input("")
        selected_files_list = []
        if nums =="0":
            sys.exit(0)
        elif "-" in nums:
            selected_files_list = dash_into_list(nums)
            if selected_files_list == []:
                new_error = 1
        else:
            input_files_list = nums.replace(".",",").replace(" ",",").replace(",",",").replace("。",",").split(",")
            for num in input_files_list:
                if num !="":
                    try:
                        num = int(num)
                    except:
                        new_error = 1
                        num = file_index + 1
                    if num > file_index:
                        new_error = 1
                    else:
                        if num not in selected_files_list:selected_files_list.append(num)
        if new_error == 0:
            loop_indicator = 1
        else:
            print("\033[31m请输入正确的序号!\033[0m")
    print("已选择:")
    for num in selected_files_list:
        print("\033[34m",num,".\033[32m ",file_list[num-1],"\033[0m")
    #开始构建vf参数序列
    select_sub_ext = ""
    while select_sub_ext=="":
        print("\033[0;32;40m请选择输入的字幕文件后缀:\033[0m")
        print("0. 不附加字幕")
        print("1. ass")
        print("2. srt")
        a = input("输入数字:")
        if a == "1":
            select_sub_ext = ".ass"
        elif a=="2":
            select_sub_ext = ".srt"
        elif a=="0" or a =="":
            select_sub_ext = "no"
        else:
            print("\033[0;31;40m输入有误,请重新输入!\033[0m")
    scale = ""
    while scale=="":
        print("\033[32m请选择视频分辨率:\033[0m")
        print("0. 保留原始分辨率[直接回车]")
        print("1. 720p")
        print("2. 1080p")
        print("3. 4K")
        a = input("输入数字:")
        if a == "1":
            scale = "-1:720"
        elif a=="2":
            scale = "-1:1080"
        elif a=="3":
            scale = "-1:2160"
        elif a=="0" or a == "":
            scale = "no"
        else:
            print("\033[34m输入有误,请重新输入!\033[0m")
    #开始构建参数序列
    args_list = []
    #追加解码器
    args_list.append("-vcodec")
    args_list.append("libx264")
    args_list.append("-acodec")
    args_list.append("libfdk_aac")
    #追加视频音频码率
    bitrate = ""
    while bitrate=="":
        print("\033[32m请选择视频/音频质量:\033[0m")
        print("0. 保留原始质量[直接回车]")
        print("1. 高质量(10M/320k)")
        print("2. 中质量(5M/320k)")
        print("3. 低质量(2M/256k)")
        a = input("输入数字:")
        if a == "1":
            bitrate = "1"
            args_list.append("-b:v")
            args_list.append("10240k")
            args_list.append("-b:a")
            args_list.append("320k")
        elif a=="2":
            bitrate = "2"
            args_list.append("-b:v")
            args_list.append("5120k")
            args_list.append("-b:a")
            args_list.append("320k")
        elif a=="3":
            bitrate = "3"
            args_list.append("-b:v")
            args_list.append("2048k")
            args_list.append("-b:a")
            args_list.append("256k")
        elif a=="0" or a == "":
            bitrate = "no"
        else:
            print("\033[34m输入有误,请重新输入!\033[0m")
    print(args_list)
    start = ""
    for file_index in selected_files_list:
        file = file_list[file_index-1]
        #获取视频文件不带后缀的文件名
        videofilename = file[:file.rfind('.')]
        #获取不带路径不带后缀的文件名(输出时使用)
        pure_videofilename = videofilename[videofilename.rfind('/')+1:]
        #初始化执行列表
        exec_list = list([ffmpeg_path, "-y", "-i"])
        print("\033[32m发现视频文件: \033[35m",file,"\033[0m")
        #file_vid_list.append(file)
        if select_sub_ext != "no":
            sub_file = videofilename + select_sub_ext
            if os.path.isfile(sub_file):
                print("\033[32m发现字幕文件: \033[35m",sub_file,"\033[0m")
                #file_ass_list.append(sub_file)
                add_sub = select_sub_ext
            else:
                add_sub = "no"
                print("\033[34m未找到视频对应的字幕文件:\033[35m",sub_file,"\033[34m本视频不添加字幕!\033[0m")
        else:
            add_sub = "no"
        if start =="":
            #开始构建执行任务:
            #追加视频文件
            exec_list.append(file)
            #追加字幕及分辨率
            if add_sub!="no" or scale!="no":
                exec_list.append("-vf")
                if add_sub==".ass":
                    vf_args = "ass=" + sub_file
                    args_connector = "," #添加参数间隔符
                elif add_sub==".srt":
                    vf_args = "subtitles=" + sub_file
                    args_connector = "," #添加参数间隔符
                else:
                    vf_args = ""
                    args_connector = ""
                if scale != "no":
                    vf_args = vf_args + args_connector + "scale=" + scale 
                exec_list.append(vf_args)
            #追加之前用户收入的自定义参数
            exec_list.extend(args_list)
            # 输出文件名
            exec_list.append(current_path + "/out/" + pure_videofilename + ".mp4")
            #开始检查输出目录是否存在
            if not os.path.exists(os.path.join(current_path, "out")):
                os.makedirs(os.path.join(current_path, "out"))
            print("\033[32m开始执行转换命令:",exec_list,"\033[0m")
            #开始最终执行
            subprocess.call(exec_list)
else:
    sys.exit(0)

下载:videoconv.py