2023-08-29_121139.png (17.47 KB, 下载次数: 0)
下载附件
2023-8-29 12:12 上传
[Python] 纯文本查看 复制代码
# -*- coding: utf-8 -*-
from flask import Flask, send_from_directory
import os
import sys
import netifaces
import random
import pyperclip
import winreg
import socket
import psutil
import time
import threading
import tkinter as tk
from PIL import ImageTk, Image
import qrcode
def start_flask_server(p,fol,cip):
app = Flask(__name__)
@app.route('/download/', methods=['GET'])
def download_file(filename):
file_path = os.path.join(fol, filename)
if not os.path.exists(file_path):
return "文件不存在", 404
return send_from_directory(fol, filename, as_attachment=True, conditional=True)
app.run(host=cip, port=p, debug=False, threaded=True)
def get_all_ips():
interfaces = netifaces.interfaces()
ip_addresses = []
for interface in interfaces:
addresses = netifaces.ifaddresses(interface)
if netifaces.AF_INET in addresses:
for address in addresses[netifaces.AF_INET]:
ip_address = address['addr']
if ip_address != '127.0.0.1':
ip_addresses.append(ip_address)
return ip_addresses
def choose_ip():
all_ips = get_all_ips()
ip_count = len(all_ips)
for i, ip in enumerate(all_ips):
print(f"{i+1}、{ip}")
while True:
selected_index = input("选择要使用的IP,并输入编号,按回车确认:")
try:
selected_index = int(selected_index)
if selected_index >= 1 and selected_index 0:
if sflj == 0:
sflj += 1
print("有连接",list(set(ips)))
return False
else:
if sflj == 0:
print("没开始连接")
return False
else:
print("连接结束")
return True
def show_qr_code(data):
window = tk.Tk()
window.title("二维码分享")
window.resizable(False, False)
window.attributes('-toolwindow', True)
qr = qrcode.QRCode(
version=1,
error_correction=qrcode.constants.ERROR_CORRECT_H,
box_size=10,
border=4,
)
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image(fill_color="black", back_color="white")
width, height = img.size
max_width = 200
max_height = 200
if width > max_width or height > max_height:
scale = min(max_width/width, max_height/height)
new_width = int(width * scale)
new_height = int(height * scale)
img = img.resize((new_width, new_height), Image.Resampling.LANCZOS)
photo = ImageTk.PhotoImage(img)
label = tk.Label(window, image=photo)
label.pack()
window.update_idletasks()
screen_width = window.winfo_screenwidth()
screen_height = window.winfo_screenheight()
window_width = window.winfo_width()
window_height = window.winfo_height()
x = int((screen_width - window_width) / 2)
y = int((screen_height - window_height) / 2)
window.geometry(f"{window_width}x{window_height}+{x}+{y}")
window.mainloop()
def sys_init(arg):
ip = cip = choose_ip()
executable_path = os.path.abspath(sys.executable)
if getattr(sys, 'frozen', False):
exe = icon = '"' + executable_path + '"'
#print("脚本已编译成exe文件")
else:
pypath = read_registry_value()
exe = '"' + pypath + '" "' + arg[0] + '"'
icon = '"' + pypath + '"'
#print("脚本未被编译成exe文件")
write_registry_entry(exe,icon,cip)
return ip
def qr_code_thread(url):
show_qr_code(url)
if __name__ == '__main__':
args = sys.argv
ip = check_reg_key("ip")
if ip == None:
ip = sys_init(args)
if len(args) - 1 == 0:
print("**请通过右键菜单运行此程序**")
print("Tips:如不需自动退出和二维码功能,请在注册表HKEY_CLASSES_ROOT\*\shell\Share File中自行修改")
hc = input("直接按回车键退出程序,输入其他任意内容后回车重新设置IP:")
if hc != "":
sys_init(args)
sys.exit(0)
file = args[1]
path = os.path.dirname(file)
filename = os.path.basename(file)
while True:
port = random.randint(10000, 65500)
if CheckPortUse(port):
break
url = "http://" + ip + ":" + str(port) + "/download/" + filename
pyperclip.copy(url)
print("\n↓↓文件下载地址已复制到剪贴板↓↓\n\n " + url + " \n")
print("↑↑文件下载地址已复制到剪贴板↑↑\n\n\n52PoJie\n")
server_thread = threading.Thread(target=start_flask_server, args=(port, path, ip))
server_thread.start()
qr = check_reg_key("showqr")
if str(qr) == "1":
print("如不需二维码功能请在注册表HKEY_CLASSES_ROOT\*\shell\Share File\showqr中自行修改")
qr_thread = threading.Thread(target=qr_code_thread, args=(url,))
qr_thread.start()
else:
print("如需要二维码功能请在注册表HKEY_CLASSES_ROOT\*\shell\Share File\showqr中自行修改值为1")
autoexit = check_reg_key("autoexit")
if str(autoexit) == "1":
print("如不需自动退出功能请在注册表HKEY_CLASSES_ROOT\*\shell\Share File\autoexit中自行修改")
sflj = 0
while True:
zt = get_port_connections(port)
if zt:
print("结束了,可以退出了")
os._exit(0)
time.sleep(1)
else:
print("如需要自动退出功能请在注册表HKEY_CLASSES_ROOT\*\shell\Share File\autoexit中自行修改值为1")