python3简单的http.server互传文件的代码,可上传文件

本文介绍如何使用Python3内置的http.server模块创建一个简单的HTTP服务器,实现文件的上传和下载。通过这个服务器,你可以方便地在本地进行文件交互操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import cgi
import http.server
import mimetypes
import os
import platform
import posixpath
import re
import shutil
import socket
import subprocess
import sys
import threading
import time
import urllib.error
import urllib.parse
import urllib.request
import uuid
from socketserver import ThreadingMixIn

try:
    import numpy as np
except:
    os.system('pip install -i https://pypi.tuna.tsinghua.edu.cn/simple numpy ')
    import numpy as np

try:
    import qrcode
except:
    os.system('pip install -i https://pypi.tuna.tsinghua.edu.cn/simple qrcode ')
    import qrcode

try:
    import PIL
except:
    os.system('pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pillow ')

try:
    from io import StringIO
except ImportError:
    from io import StringIO


class GetWanIp:
    def getip(self):
        match_ip_dict = {
   
   }
        ipconfig_result_list = os.popen('ipconfig').readlines()
        for i in range(0, len(ipconfig_result_list)):
            if re.search(r'IPv4 地址', ipconfig_result_list[i]) != None:
                match_ip = re.search(r'\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',
                                     ipconfig_result_list[i]).group(0)
                for j in range(3, 7):
                    if re.search(r"无线局域网适配器", ipconfig_result_list[i - j]) != None:
                        match_ip_dict[ipconfig_result_list[i - j]] = match_ip
        ip_dict = match_ip_dict
        for i in ip_dict:
            return ip_dict[i]

    def visit(self, url):
        opener = urllib.request.urlopen(url, None, 3)
        if url == opener.geturl():
            str = opener.read()
        return re.search(r'(\d+\.){3}\d+', str).group(0)


def showTips():
    print("")
    print('----------------------------------------------------------------------->> ')
    try:
        port = int(sys.argv[1])
    except Exception as e:
        print('-------->> 警告:端口未给出,将使用默认端口: 8080 ')
        print('-------->> 如需使用其他端口,请执行: ')
        print('-------->> python HTTPServerWithUpload.py port ')
        print("-------->> port是一个整数,它的范围是: 1024 < port < 65535 ")
        port = 8080

    if not 1024 < port < 65535:
        port = 8080
    print('-------->> 现在,在监听 ' + str(port) + ' 端口 ...')
    osType = platform.system()
    data = 'http://127.0.0.1:' + str(port)
    if osType == "Windows":
        print('-------->> 您可以访问地址URL:http://' +
              GetWanIp().getip() + ':' + str(port))
        data = 'http://'+GetWanIp().getip() + 
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值