如何制作自己的网站并且插口代码(怎么用Python写一个网页程序,上传文件,处理完毕,下载下来)

建站教程 1年前 (2023) admin
63 0

怎么用Python写一个网页程序,上传文件,处理完毕,下载下来

直接上代码1、服务器接口import flask, os,sys,timefrom flask import request, send_from_directoryinterface_path = os.path.dirname(__file__)sys.path.insert(0, interface_path) #将当前文件的父目录加入临时系统变量server = flask.Flask(__name__)#get方法:指定目录下载文件@server.route('/download', methods=['get'])def download():fpath = request.values.get('path', '') #获取文件路径fname = request.values.get('filename', '') #获取文件名if fname.strip() and fpath.strip():print(fname, fpath)if os.path.isfile(os.path.join(fpath,fname)) and os.path.isdir(fpath):return send_from_directory(fpath, fname, as_attachment=True) #返回要下载的文件内容给客户端else:return '{"msg":"参数不正确"}'else:return '{"msg":"请输入参数"}'# get方法:查询当前路径下的所有文件@server.route('/getfiles', methods=['get'])def getfiles():fpath = request.values.get('fpath', '') #获取用户输入的目录print(fpath)if os.path.isdir(fpath):filelist = os.listdir(fpath)files = [file for file in filelist if os.path.isfile(os.path.join(fpath, file))]return '{"files":"%s"}' % files# post方法:上传文件的@server.route('/upload', methods=['post'])def upload():fname = request.files.get('file') #获取上传的文件if fname:t = time.strftime('%Y%m%d%H%M%S')new_fname = r'upload/' + t + fname.filenamefname.save(new_fname) #保存文件到指定路径return '{"code": "ok"}'else:return '{"msg": "请上传文件!"}'server.run(port=8000, debug=True)2、客户端请求import requestsimport os#上传文件到服务器file = {'file': open('hello.txt','rb')}r = requests.post(':8000/upload', files=file)print(r.text)#查询fpath下的所有文件r1 = requests.get(':8000/getfiles',data={'fpath': r'download/'})print(r1.text)#下载服务器download目录下的指定文件r2 = requests.get(':8000/download',data={'filename':'hello_upload.txt', 'path': r'upload/'})file = r2.text #获取文件内容basepath = os.path.join(os.path.dirname(__file__), r'download/')with open(os.path.join(basepath, 'hello_download.txt'),'w',encoding='utf-8') as f: #保存文件f.write(file)

在网站上面放一个淘宝的接口需要多少钱

一般来说,各个平台一个单的收入不同,自已垫资跟商家出资价格又不同。有宣传说,淘宝刷一单多的可以赚20块,甚至50块,其实这些很多都是骗人,出这么高的价格,商家伤不起啊。或者也有真的,但那是需要刷手出资购买价格数百上千的商品的,比如家具、高档数码之类的,需要一定的资金才能玩得起,最主要是太不安全了,新闻里受骗上当的以这类居多。

阅读全文 >

京东电子面单怎么申请的

在快递鸟网站先申请一个电子面单接口KEY先,然后技术开发一下就可以设置到网站了,不过还要和京东申请一个电子面单打印的账号。

谁能告诉我怎么把支付宝接口弄到个人网站上去啊

把支付宝图片直接放在个人网站上没有什么作用,同时也涉及侵权了。一般有支付宝图标的网站是他们与支付宝有合作关系,在该网站上进行交易可以使用支付宝交易,并不是把图片放上去的。

自己的网站如何使用别人的api接口

自己的网站使用别人的api接口,你可以和对方商议,然后遵从对方的规定,如果对方同意,就可以使用。

相关文章