实例介绍
【实例简介】
python制作的局域网传输文件-适用于windows平台,将电脑变为共享文件神器,手机开热点或者电脑开热点,连接后实现手机电脑一键互联,快速传输分享文件,摆脱没有数据线的烦恼,项目里面含有可执行文件以及项目源码。
【实例截图】
【核心代码】
# -*- coding:utf-8 -*-
import psutil
import qrcode
from tkinter import *
from PIL import ImageTk
from flask import *
import os,json
import tkinter as tk # 使用Tkinter前需要先导入
import threading
import tkinter.messagebox
import logging
app = Flask(__name__)
app.config.from_object(__name__)
#禁用控制台
log = logging.getLogger('werkzeug')
log.setLevel(logging.ERROR)
def GetNameByEveryDir(file_dir):
# Input Root Dir and get all img in per Dir.
# Out Every img with its filename and its dir and its path
FileNameWithPath = []
FileName = []
FileDir = []
for root, dirs, files in os.walk(file_dir):
for file in files:
FileNameWithPath.append(os.path.join(root, file)) # 保存图片路径
FileName.append(file) # 保存图片名称
FileDir.append(root[len(file_dir):]) # 保存图片所在文件夹
return FileName,FileNameWithPath,FileDir
# 首页
@app.route('/')
def index():
render_template('up_video.html',file_list=ret,all_file=len(FileName),pages=page,all_page=all_page)
@app.route('/next_page/<int:page>/', methods=['GET'])
def next_page(page):
filepath = os.path.join(os.path.dirname(os.path.abspath('__file__')),'phone_win_file\\')
if not os.path.exists(filepath):
os.makedirs(filepath)
FileName,FileNameWithPath,FileDir = GetNameByEveryDir(filepath)
limit = 10
all_page=int(len(FileName)/limit) 1
pages = int(page)
if pages<=0:
pages=1
elif pages>=all_page:
pages=int(all_page)
else:
pages=int(page)
start = (pages - 1) * limit
end = pages * limit if len(FileName) > pages * limit else len(FileName)
ret = [FileName[i] for i in range(start, end)]
return render_template('up_video.html',file_list=ret,all_file=len(FileName),pages=pages,all_page=all_page)
@app.route('/phone_win_file/<file_name>', methods=['GET'])
def phone_win_file(file_name):
try:
except Exception as e:
return json.dumps({'code': "502"}, ensure_ascii=False)
@app.route('/up_video', methods=['post'])
def up_video():
try:
except Exception as e:
print(e)
return json.dumps({'code': "502"}, ensure_ascii=False)
def run_sever():
app.run(host='0.0.0.0',port=5000,debug=False,use_reloader=False)
def start():
global l,l1,count,ip_list,len_list,chiose
if chiose==0:
thre = threading.Thread(target=run_sever) # 创建一个线程运行服务器
thre.setDaemon(True)
thre.start() # 运行线程
l1.config(text=ip_list[count])
img=qrc_img(ip_list[count])
IMG=ImageTk.PhotoImage(img)
l.config(image=IMG)
l.image=IMG #keep a reference
count =1
chiose=1
tkinter.messagebox.showinfo('成功','已开启服务!')
else:
tkinter.messagebox.showinfo('提示','服务已运行!')
def getIP():
"""获取ipv4地址"""
dic = psutil.net_if_addrs()
ipv4_list = []
for adapter in dic:
#网线端口:以太网,wifi端口:WLAN
if '以太' in adapter or 'WLAN' in adapter:
snicList = dic[adapter]
for snic in snicList:
if snic.family.name == 'AF_INET':
ipv4 = snic.address
if ipv4 != '127.0.0.1':
ipv4_list.append("http://" ipv4 ":5000")
if len(ipv4_list)>=1:
return ipv4_list[::-1],len(ipv4_list)
else:
return []
def qrc_img(url):
qr=qrcode.QRCode(version = 2,error_correction = qrcode.constants.ERROR_CORRECT_L,box_size=11,border=2,)
qr.add_data(url)
qr.make(fit=True)
img = qr.make_image()
return img
def show_qrc():
pass
def open_dir():
filepath = os.path.join(os.path.dirname(os.path.abspath('__file__')),'phone_win_file\\')
if not os.path.exists(filepath):
os.makedirs(filepath)
os.system("start explorer %s" % filepath)
def about():
tkinter.messagebox.showinfo('帮助','开启服务后手机扫描显示的二维码等待页面打开,\n页面打不开说明有多个网口,\n需要切换二维码进行尝试。\n进入页面查看电脑下的文件,点击文件名下载文件到手机。\n选择手机页面中的上传文件将手机文件传到手机。\n作者:Try it.(Cxk)')
def connect():
import webbrowser
webbrowser.open("https://cxk-life.blog.csdn.net/")
if __name__ == '__main__':
root=Tk()
# 设置窗口大小
winWidth = 600
winHeight = 400
# 获取屏幕分辨率
screenWidth = root.winfo_screenwidth()
screenHeight = root.winfo_screenheight()
x = int((screenWidth - winWidth) / 2)
y = int((screenHeight - winHeight) / 2)
# 设置主窗口标题
root.title("局域网传输文件")
# 设置窗口初始位置在屏幕居中
root.geometry("%sx%s %s %s" % (winWidth, winHeight, x, y))
# 设置窗口图标
root.iconbitmap("./img/cxk.ico")
# 设置窗口宽高固定
root.resizable(0, 0)
# 添加菜单栏
f = tkinter.Menu(root)
root['menu']=f
f.add_command(label='关于',command=about)
f.add_command(label='联系作者',command=connect)
#增加背景图片
photo = tk.PhotoImage(file="./img/no_bg.png")
theLabel = tk.Label(root,text="",justify=tk.LEFT,image=photo,compound = tk.CENTER)
theLabel.place(relx=0.8, rely=0.63, anchor=CENTER)
count=0
chiose=0
ip_list,len_list=getIP()
Button(root,text='开启服务',command=start).place(relx=0.8, rely=0.08, anchor=CENTER)
Button(root,text='更换网址',command=show_qrc).place(relx=0.7, rely=0.2, anchor=CENTER)
Button(root,text='文件所在',command=open_dir).place(relx=0.9, rely=0.2, anchor=CENTER)
l=Label(root)
l.place(relx=0.3, rely=0.5, anchor=CENTER)
l1=Label(root)
l1.place(relx=0.3, rely=0.05, anchor=CENTER)
root.mainloop()
小贴士
感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。
- 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
- 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
- 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
- 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。
关于好例子网
本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明
网友评论
我要评论