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() + ':' +
python3简单的http.server互传文件的代码,可上传文件
最新推荐文章于 2024-05-20 14:16:48 发布