使用TFTP协议(Trivial File Transfer Protocol,简单文件传输协议)下载服务器指定文件夹内的图片
from socket import * # 若import socket,后续写法为socket.socket()、socket.gethostname()
import struct # struct模块可以按照指定格式将Python数据转换为字符串,该字符串为字节流
# TFTP(Trivial File Transfer Protocol,简单文件传输协议),使用这个协议,就可以实现简单文件的下载,tftp端口号为69。
from icecream import ic
import os
import random
os.chdir(r'D:\百战python\Python网络与并发编程——网络\tftp模拟下载的图片') # 指定路径
path = os.getcwd()
ic(path)
file_list = os.listdir(path) # listdir(path),返回 path 目录下的文件名。
ic(file_list)
filename = random.choice(file_list) # 随机抽取即将下载的文件名。常见的图片格式都是可以的
ic(filename)
ic(gethostname(