1. 程式人生 > 實用技巧 >使用python批量獲取交換機的序列號、版本等資訊生成excel表

使用python批量獲取交換機的序列號、版本等資訊生成excel表

一、Java基本的網路支援

java提供了java.net這個包。

1、使用InetAddress

2、使用URLDecoder和URLEncoder

上面這個%E7%94%98%E5%87%8C%E6%98%8A是一種application/x-www-form-urlencoded MIME格式的字串。

3、URL、URLConnection、URLPermission

網路地址的基本組成:

protocol :// hostname[:port] / path / [;parameters][?query]#fragment

下載檔案的工具類:

package network.download;

import java.io.InputStream;
import java.io.RandomAccessFile;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * @author:Gan Linghao
 * @date:2020/8/3 0003
 * @time:15:31
 * @description:no description
 */
public class DownUtil
{
    // 定義下載資源的路徑
    private String path;
    // 指定所下載的檔案的儲存位置
    private String targetFile;
    // 定義需要使用多少執行緒下載資源
    private int threadNum;
    // 定義下載的執行緒物件
    private DownThread[] threads;
    // 定義下載的檔案的總大小
    private int fileSize;

    public DownUtil(String path, String targetFile, int threadNum)
    {
        this.path = path;
        this.threadNum = threadNum;
        // 初始化threads陣列
        threads = new DownThread[threadNum];
        this.targetFile = targetFile;
    }

    public void download() throws Exception
    {
        URL url = new URL(path);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setConnectTimeout(5 * 1000);
        conn.setRequestMethod("GET");
        conn.setRequestProperty(
                "Accept",
                "image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
                        + "application/x-shockwave-flash, application/xaml+xml, "
                        + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "
                        + "application/x-ms-application, application/vnd.ms-excel, "
                        + "application/vnd.ms-powerpoint, application/msword, */*");
        conn.setRequestProperty("Accept-Language", "zh-CN");
        conn.setRequestProperty("Charset", "UTF-8");
        conn.setRequestProperty("Connection", "Keep-Alive");
        // 得到檔案大小
        fileSize = conn.getContentLength();
        conn.disconnect();
        int currentPartSize = fileSize / threadNum + 1;
        RandomAccessFile file = new RandomAccessFile(targetFile, "rw");
        // 設定本地檔案的大小
        file.setLength(fileSize);
        file.close();
        for (int i = 0; i < threadNum; i++)
        {
            // 計算每條執行緒的下載的開始位置
            int startPos = i * currentPartSize;
            // 每個執行緒使用一個RandomAccessFile進行下載
            RandomAccessFile currentPart = new RandomAccessFile(targetFile,
                    "rw");
            // 定位該執行緒的下載位置
            currentPart.seek(startPos);
            // 建立下載執行緒
            threads[i] = new DownThread(startPos, currentPartSize,
                    currentPart);
            // 啟動下載執行緒
            threads[i].start();
        }
    }

    // 獲取下載的完成百分比
    public double getCompleteRate()
    {
        // 統計多條執行緒已經下載的總大小
        int sumSize = 0;
        for (int i = 0; i < threadNum; i++)
        {
            sumSize += threads[i].length;
        }
        // 返回已經完成的百分比
        return sumSize * 1.0 / fileSize;
    }

    private class DownThread extends Thread
    {
        // 當前執行緒的下載位置
        private int startPos;
        // 定義當前執行緒負責下載的檔案大小
        private int currentPartSize;
        // 當前執行緒需要下載的檔案塊
        private RandomAccessFile currentPart;
        // 定義已經該執行緒已下載的位元組數
        public int length;

        public DownThread(int startPos, int currentPartSize,
                          RandomAccessFile currentPart)
        {
            this.startPos = startPos;
            this.currentPartSize = currentPartSize;
            this.currentPart = currentPart;
        }

        @Override
        public void run()
        {
            try
            {
                URL url = new URL(path);
                HttpURLConnection conn = (HttpURLConnection)url
                        .openConnection();
                conn.setConnectTimeout(5 * 1000);
                conn.setRequestMethod("GET");
                conn.setRequestProperty(
                        "Accept",
                        "image/gif, image/jpeg, image/pjpeg, image/pjpeg, "
                                + "application/x-shockwave-flash, application/xaml+xml, "
                                + "application/vnd.ms-xpsdocument, application/x-ms-xbap, "
                                + "application/x-ms-application, application/vnd.ms-excel, "
                                + "application/vnd.ms-powerpoint, application/msword, */*");
                conn.setRequestProperty("Accept-Language", "zh-CN");
                conn.setRequestProperty("Charset", "UTF-8");
                InputStream inStream = conn.getInputStream();
                // 跳過startPos個位元組,表明該執行緒只下載自己負責哪部分檔案。
                inStream.skip(this.startPos);
                byte[] buffer = new byte[1024];
                int hasRead = 0;
                // 讀取網路資料,並寫入本地檔案
                while (length < currentPartSize
                        && (hasRead = inStream.read(buffer)) != -1)
                {
                    currentPart.write(buffer, 0, hasRead);
                    // 累計該執行緒下載的總大小
                    length += hasRead;
                }
                currentPart.close();
                inStream.close();
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
        }
    }
}

測試方法(可脫離工具類):

public class Test {
    private final static String path = "https://glh508-blog-img.oss-cn-beijing.aliyuncs.com/1596438480944.png";
    public static void test() throws IOException {
        // (1)建立URL例項
        URL url = new URL(path);
        // (2)跟網路地址建立連線
        HttpURLConnection connection = (HttpURLConnection)url.openConnection();
        int code = connection.getResponseCode();
        String msg = connection.getResponseMessage();
        System.out.println(code + ":" + msg);
        //(3)互動
        InputStream input = connection.getInputStream();
        FileOutputStream output = new FileOutputStream("code/network/demo.png");
        int a = -1;
        while ((a = input.read()) != -1){
            output.write(a);
        }

        // (4)關閉資源
        input.close();
        output.close();
        connection.disconnect();
    }


    public static void main(String[] args) throws Exception {
//        DownUtil downUtil = new DownUtil("https://glh508-blog-img.oss-cn-beijing.aliyuncs.com/1596438480944.png",
//                "demo.png",4);
//        downUtil.download();
        test();
    }
}

步驟:

(1)、建立URL物件;

(2)、與網路資源建立連線。用的是URLConnection的子類HttpURLConnection;對獲得連線進行一些請求引數的操作。

(3)、獲取輸入流,對輸入流進行寫出操作;

(4)、關閉資源。

二、基於TCP的網路程式設計

端對端協議:A與B裝置之間建立一條虛擬的網路鏈路。(用到Java提供的一個物件Socket)

三、基於UDP的網路程式設計

四、作業

五、UDP詳解

  一,TCP/IP協議棧中,TCP協議和UDP協議的聯絡和區別?

    聯絡:

      TCP和UDP是TCP/IP協議棧中傳輸層的兩個協議,它們使用網路層功能把資料包傳送到目的地,從而為應用層提供網路服務。

    區別:

  1. TCP是面向連線的傳輸。UDP是無連線的傳輸。

    TCP保證資料按照發送順序到達,UDP無法保證。

    1. TCP是可靠性傳輸,而UDP則是不可靠傳輸。

      UDP因為少了很多控制資訊,所以傳輸速度比TCP速度快。

            6. TCP適合用於傳輸大量資料,UDP適合用於傳輸小量資料。

      舉例: TCP的server和client之間通訊就好比兩個人打電話。UDP的server和client之間的通訊就像兩個人發電報或者發簡訊。

  二,UDP通訊協議的特點:

  1. 將資料極封裝為資料包,面向無連線。

    每個資料包大小限制在64K中

      3.因為無連線,所以不可靠

      4. 因為不需要建立連線,所以速度快

      5.udp 通訊是不分服務端與客戶端的,只分傳送端與接收端

  三,怎樣來編寫UDP?

      傳送資料步驟:

        1.建立socket服務

        2.建立資料包

        3.將資料封裝到資料包中,新增ip和埠以及資料

        4.傳送

        5.關閉資源

      接收資料步驟:

        1.建立socket服務,並監聽埠

        2.建立資料包,用來接收資料

        3.用socket接收資料到資料包中

        4.從資料包中取出資料

        5.關閉資源

  四,程式碼實現UDP通訊

1.客戶端程式碼

 1 import java.io.IOException;
 2 import java.net.DatagramPacket;
 3 import java.net.DatagramSocket;
 4 import java.net.InetAddress;
 5 import java.util.Scanner;
 6 
 7 public class User {
 8     static Scanner sc=  new Scanner(System.in);
 9     public static void main(String[] args) throws IOException {
10         //1.建立客戶端套接字
11         DatagramSocket ds = new DatagramSocket();
12         //2.建立客戶端傳送資料包
13         while(true){
14             System.out.println("請輸入要傳送的資訊:");
15             String info = sc.nextLine();
16             byte []buf =info.getBytes();
17             DatagramPacket dp = new DatagramPacket(buf, buf.length, 
18                     InetAddress.getByName("localhost"), 152);
19             //3.傳送資料包
20             ds.send(dp);
21             //4.結束髮送迴圈
22             if("886".equals(info)){
23                 break;
24             }
25         }
26         //5.關閉套接字
27         ds.close();
28     }
29 }

    2.服務端程式碼

 1 import java.io.IOException;
 2 import java.net.DatagramPacket;
 3 import java.net.DatagramSocket;
 4 import java.net.InetAddress;
 5 
 6 public class Server {
 7       public static void main(String[] args) throws IOException {
 8         //1.建立服務端套接字
 9         DatagramSocket ds = new DatagramSocket(152);//注意指定埠
10         //2.建立接受客戶端資訊的空資料包
11         while(true){
12             byte [] buf =new byte[1024];
13             DatagramPacket dp = new DatagramPacket(buf, buf.length);
14             //3.接受資料
15             ds.receive(dp);
16             //4.拆分資料
17             byte[] data = dp.getData();
18             //5.獲取客戶端IP和主機名
19             InetAddress ip = dp.getAddress();
20             String host = ip.getHostName();
21             //6.讀取資料
22             String info = new String(buf,0,buf.length);
23             System.out.println("來自"+host+"的訊息是:"+info);
24             ds.close();
25             //7.關閉套接字
26         }
27     }
28 }


CNAME驗證

請將6Eaj2uriTt.yangxl.xyz使用CNAME解析到ziyuan.baidu.com

完成操作後請點選“完成驗證”按鈕。

為保持驗證通過的狀態,成功驗證後請不要刪除該DNS記錄