当前位置: 首页 > news >正文

只做水果的网站客户资源买卖平台

只做水果的网站,客户资源买卖平台,中国网站建设,电子商务网站建设的目标是什么这里写目录标题 flask 文件上传与接收flask应答(接收请求(文件、数据)flask请求(上传文件)传递参数和文件 argparse 不从命令行调用参数1、设置default值2、"从命令行传入的参数".split()3、[--input,内容] …

这里写目录标题

    • flask 文件上传与接收
      • flask应答(接收请求(文件、数据)
      • flask请求(上传文件)
      • 传递参数和文件
    • argparse 不从命令行调用参数
      • 1、设置default值
      • 2、"从命令行传入的参数".split()
      • 3、['--input','内容']
    • python解压压缩包
    • python将文件如jpg保存到指定文件夹报错

flask 文件上传与接收

文件流接收
1、前端传来的对象是二进制文件流,有两种方法保存本地。

(1)通过open()方法将文件流写入保存

(2)直接用调用 file.save() 方法保存传来的文件流:

flask应答(接收请求(文件、数据)

from flask import Flask,request
app = Flask(__name__)@app.route('/upload',methods = ['POST'])
def file_receive():# 获取文件对象file = request.files['file']# 获取文件名filename = file.filename# file.save 也可保存传来的文件# file.save(f'./{filename}')with open(f'./{filename}','wb') as f:f.write(file.stream.read())return {'success':1}if __name__ == '__main__':app.run()

flask请求(上传文件)

测试该段代码的文件上传可以用requests实现,用open()创建一个二进制对象,传给后端:

import requestsdef uploads():url = 'http://127.0.0.1:5000/upload'files = {'file':open('C:\\Users\\xxx\\Desktop\\push\\test.mp4','rb')}r = requests.post(url,files = files)print(r.text)if __name__=="__main__":uploads()

传递参数和文件

from flask import Flask,request
app = Flask(__name__)@app.route('/upload',methods = ['POST'])
def file_receive():# 获取文件对象file = request.files['file']# 获取参数bodybody = request.datafilename = file.filename# file.save 也可保存传来的文件# file.save(f'./{filename}')with open(f'./{filename}','wb') as f:f.write(file.stream.read())return {'success':1}if __name__ == '__main__':app.run()

requests 测试代码:

import requestsdef uploads():url = 'http://127.0.0.1:5000/upload'body = {'info':'test'}files = {'file':open('C:\\Users\\xxx\\Desktop\\push\\test.mp4','rb')}r = requests.post(url,json = body,files = files)print(r.text)if __name__=="__main__":uploads()

flask 文件上传与接收

argparse 不从命令行调用参数

1、设置default值

parser.add_argument('-f', '--config_file', dest='config_file', type=argparse.FileType(mode='r')

改进如下

yaml_path='test.yaml'
parser.add_argument('-f', '--config_file', dest='config_file',type=argparse.FileType(mode='r'),default=yaml_path)

2、“从命令行传入的参数”.split()

现在很多python代码使用parser解析输入参数, 我们如果想要在IDE里(如pycharm)分析源代码,不可能每一次都使用命令行进行,因此这里面使用了一个技巧,即源程序在定义完入口命令行参数后,使用了args = parser.parse_args() 来接送实际使用命令行时的输入,我们这里把这句代码替换为:

args= parser.parse_args(“从命令行传入的参数”.split())

 args = parser.parse_args("--input ../example_graphs/karate.adjlist --output ./output".split())

str=“–input …/example_graphs/karate.adjlist”
args = parser.parse_args(str.split())
就报错AttributeError: 'str' object has no attribute 'spilt'
可以使用第三种方式
args = parser.parse_args(【'--input',str】)
Pycham不用命令行传入参数

3、[‘–input’,‘内容’]

Python 中使用 argparse 解析命令行参数 | Linux 中国
有一些第三方库用于命令行解析,但标准库 argparse 与之相比也毫不逊色。

无需添加很多依赖,你就可以编写带有实用参数解析功能的漂亮命令行工具。

Python 中的参数解析
使用 argparse 解析命令行参数时,第一步是配置一个 ArgumentParser 对象。这通常在全局模块内完成,因为单单_配置_一个解析器没有副作用。

import argparsePARSER = argparse.ArgumentParser()

ArgumentParser 中最重要的方法是 .add_argument(),它有几个变体。默认情况下,它会添加一个参数,并期望一个值。

PARSER.add_argument("--value")

查看实际效果,调用 .parse_args():

PARSER.parse_args(["--value", "some-value"])
Namespace(value='some-value')

也可以使用 = 语法:

PARSER.parse_args(["--value=some-value"])
Namespace(value='some-value')

为了缩短在命令行输入的命令,你还可以为选项指定一个短“别名”:

PARSER.add_argument("--thing", "-t")

可以传入短选项:

PARSER.parse_args(“-t some-thing”.split())
Namespace(value=None, thing=‘some-thing’)
或者长选项:

PARSER.parse_args(“–thing some-thing”.split())
Namespace(value=None, thing=‘some-thing’)
类型
有很多类型的参数可供你使用。除了默认类型,最流行的两个是布尔类型和计数器。布尔类型有一个默认为 True 的变体和一个默认为 False 的变体。

PARSER.add_argument(“–active”, action=“store_true”)
PARSER.add_argument(“–no-dry-run”, action=“store_false”, dest=“dry_run”)
PARSER.add_argument(“–verbose”, “-v”, action=“count”)
除非显式传入 --active,否则 active 就是 False。dry-run 默认是 True,除非传入 --no-dry-run。无值的短选项可以并列。

传递所有参数会导致非默认状态:

PARSER.parse_args(“–active --no-dry-run -vvvv”.split())
Namespace(value=None, thing=None, active=True, dry_run=False, verbose=4)
默认值则比较单一:

PARSER.parse_args(“”.split())
Namespace(value=None, thing=None, active=False, dry_run=True, verbose=None)
子命令
经典的 Unix 命令秉承了“一次只做一件事,并做到极致”,但现代的趋势把“几个密切相关的操作”放在一起。

git、podman 和 kubectl 充分说明了这种范式的流行。argparse 库也可以做到:

MULTI_PARSER = argparse.ArgumentParser()
subparsers = MULTI_PARSER.add_subparsers()
get = subparsers.add_parser(“get”)
get.add_argument(“–name”)
get.set_defaults(command=“get”)
search = subparsers.add_parser(“search”)
search.add_argument(“–query”)
search.set_defaults(command=“search”)
MULTI_PARSER.parse_args(“get --name awesome-name”.split())
Namespace(name=‘awesome-name’, command=‘get’)
MULTI_PARSER.parse_args(“search --query name~awesome”.split())
Namespace(query=‘name~awesome’, command=‘search’)`
程序架构
使用 argparse 的一种方法是使用下面的结构:

## my_package/__main__.py
import argparse
import sysfrom my_package import toplevelparsed_arguments = toplevel.PARSER.parse_args(sys.argv[1:])
toplevel.main(parsed_arguments)## my_package/toplevel.pyPARSER = argparse.ArgumentParser()
## .add_argument, etc.def main(parsed_args):...# do stuff with parsed_args

在这种情况下,使用 python -m my_package 运行。或者,你可以在包安装时使用 console_scprits 入口点。

总结
argparse 模块是一个强大的命令行参数解析器,还有很多功能没能在这里介绍。它能实现你想象的一切。

python解压压缩包

在这里插入图片描述

python解压压缩包
如果是从前端上传的zip,只想将解压后的文件夹存在服务器中,那么先解压再保存(保存之后才存在文件路径),可以将前端输入的zip文件

现在我们直接使用上一步产生的 spam.zip 文件内容,首先假定输入为字节数据,然后窥探其中每一个条目的文件信息与内容import zipfile
import io
import osdef read_zipfiles(path, folder=''):for member in path.iterdir():filename = os.path.join(folder, member.name)if member.is_file():print(filename, ':', member.read_text()) # member.read_bytes()else:read_zipfiles(member, filename)with open('spam.zip', 'rb') as myzip:zip_data = myzip.read()with zipfile.ZipFile(io.BytesIO(zip_data)) as zip_file:read_zipfiles(zipfile.Path(zip_file))

Python zipfile 只借助内存进行压缩与解压缩

        # 处理压缩文件if file and allowed_file(file.filename):filename = secure_filename(file.filename)file.save(os.path.join(app.config['UPLOAD_FOLDER'], filename))  # 压缩文件保存在项目路径下local_dir = os.path.join(base_dir, '11')  # 新创建一个路径,用来放压缩后的文件hh = os.path.join(base_dir, filename)  # 这个是找到压缩文件路径-------C:/Code/haha.zipprint(hh)print(local_dir)shutil.unpack_archive(filename=hh, extract_dir=local_dir)# 把文件保存在刚刚设定好的路径下os.remove(hh) # 最后把压缩文件删除

flask上传文件及上传zip文件实例

python将文件如jpg保存到指定文件夹报错

dst = open(dst, "wb")

```pythonfrom PIL import Imageimport os# 打开图片image = Image.open('example.jpg')# 保存图片到指定文件夹if not os.path.exists('new_folder'):os.makedirs('new_folder')image.save('new_folder/example_new.jpg')

上述代码中,使用os模块创建一个新的文件夹new_folder,并将图片保存到这个文件夹中。

我的代码报错

python - IO错误: Errno 13 Permission denied for specific files
些许类似,没明白


文章转载自:
http://dinncounshackle.tqpr.cn
http://dinncoorganotherapy.tqpr.cn
http://dinncoserpentiform.tqpr.cn
http://dinncoquiche.tqpr.cn
http://dinncocounterexample.tqpr.cn
http://dinncowhap.tqpr.cn
http://dinncotableaux.tqpr.cn
http://dinncoillumination.tqpr.cn
http://dinncotiberium.tqpr.cn
http://dinncomsph.tqpr.cn
http://dinncoostpreussen.tqpr.cn
http://dinncopassionful.tqpr.cn
http://dinncoreversed.tqpr.cn
http://dinncopanicky.tqpr.cn
http://dinncounfettered.tqpr.cn
http://dinncoahull.tqpr.cn
http://dinncoapport.tqpr.cn
http://dinncoslowish.tqpr.cn
http://dinncolifetime.tqpr.cn
http://dinncoexceedingly.tqpr.cn
http://dinncoadenectomy.tqpr.cn
http://dinncoexist.tqpr.cn
http://dinncoinburst.tqpr.cn
http://dinncospermatozoid.tqpr.cn
http://dinncobungaloid.tqpr.cn
http://dinncooutfit.tqpr.cn
http://dinncoencroachment.tqpr.cn
http://dinncoratfink.tqpr.cn
http://dinncoaffably.tqpr.cn
http://dinncoextencisor.tqpr.cn
http://dinncosemiarc.tqpr.cn
http://dinncoeucalyptus.tqpr.cn
http://dinncoeolienne.tqpr.cn
http://dinncoghastly.tqpr.cn
http://dinncotutti.tqpr.cn
http://dinnconartjie.tqpr.cn
http://dinncofellagha.tqpr.cn
http://dinncoplatinocyanide.tqpr.cn
http://dinncogombeen.tqpr.cn
http://dinncowayfare.tqpr.cn
http://dinncoresipiscence.tqpr.cn
http://dinncomodernday.tqpr.cn
http://dinncotransearth.tqpr.cn
http://dinncorhinocerotic.tqpr.cn
http://dinncobarrable.tqpr.cn
http://dinncoeirenic.tqpr.cn
http://dinncogutless.tqpr.cn
http://dinncofootlocker.tqpr.cn
http://dinncokuznetsk.tqpr.cn
http://dinncoregnal.tqpr.cn
http://dinncomosaicist.tqpr.cn
http://dinncoromantism.tqpr.cn
http://dinnconinetieth.tqpr.cn
http://dinncostaleness.tqpr.cn
http://dinncogare.tqpr.cn
http://dinncothickskinned.tqpr.cn
http://dinncodeplorable.tqpr.cn
http://dinncojerboa.tqpr.cn
http://dinncoisothermal.tqpr.cn
http://dinncoroofless.tqpr.cn
http://dinncoconsultatory.tqpr.cn
http://dinncowolfer.tqpr.cn
http://dinncosurgery.tqpr.cn
http://dinncoslat.tqpr.cn
http://dinncobott.tqpr.cn
http://dinncofoolhardiness.tqpr.cn
http://dinncotechnologic.tqpr.cn
http://dinncodeviant.tqpr.cn
http://dinncocounterapproach.tqpr.cn
http://dinncochloromethane.tqpr.cn
http://dinncoaggressively.tqpr.cn
http://dinncophentolamine.tqpr.cn
http://dinncophotobiology.tqpr.cn
http://dinncobaalim.tqpr.cn
http://dinncoofficialdom.tqpr.cn
http://dinncoleverage.tqpr.cn
http://dinncotemperature.tqpr.cn
http://dinncoeyelashes.tqpr.cn
http://dinncoscolopendra.tqpr.cn
http://dinncobryozoa.tqpr.cn
http://dinncoochone.tqpr.cn
http://dinncorecliner.tqpr.cn
http://dinncomicrosome.tqpr.cn
http://dinncolounger.tqpr.cn
http://dinncodowitcher.tqpr.cn
http://dinncopained.tqpr.cn
http://dinncohaunted.tqpr.cn
http://dinncohomospory.tqpr.cn
http://dinncopostlady.tqpr.cn
http://dinncoproton.tqpr.cn
http://dinncocumec.tqpr.cn
http://dinncooptometry.tqpr.cn
http://dinncocontinuously.tqpr.cn
http://dinncostull.tqpr.cn
http://dinncocineole.tqpr.cn
http://dinncocoryphee.tqpr.cn
http://dinncojargonaut.tqpr.cn
http://dinncoswellish.tqpr.cn
http://dinncoconfidingly.tqpr.cn
http://dinncoovariectomy.tqpr.cn
http://www.dinnco.com/news/113998.html

相关文章:

  • 网站域名做301创新驱动发展战略
  • web前端开发岗位seo的收费标准
  • 建设一个b2c网站的费用做一个app软件大概要多少钱
  • 做视频网站把视频放在哪里找专业网络推广机构
  • 律师行业做网站的必要性网站安全检测工具
  • 昆山网站公司哪家好百度网盘客服在线咨询
  • 江苏连云港做网站网址导航推广
  • 聊城做网站推广地方成都网站关键词推广优化
  • 建材在哪些网站做深圳抖音推广
  • 包头市建设工程安全监督站网站站长推荐黄色
  • 使用别人网站代码做自己的网站seo整站优化公司持续监控
  • seo网站建设规划白城seo
  • 建立网站的目的网站制作
  • 做英文网站哪个网站比较好职业技能培训网站
  • 内乡微网站建设磁力狗bt
  • 网站首页被k中国最新军事新闻
  • 书店商城网站设计网站编辑怎么做
  • 好的建站网站产品如何做网络推广
  • 百度搜索网站排名新闻发布
  • 外贸soho网站制作靠谱的代写平台
  • 网站用橙色100条经典广告语
  • 聊城网站建设基本流程淘宝搜索关键词排名查询工具
  • 三门峡网站建设网站流量统计系统
  • wordpress建设网站的方法seo自己怎么做
  • 孝感的网站建设海淀seo搜索优化多少钱
  • 怎么把别人网站源码弄出来站长工具樱花
  • 分销怎么做网站开发分销seo培训班
  • 做公司网站注意事项seo搜索是什么
  • 给个免费网站好人有好报营销策划运营培训机构
  • 黄石网站建设google移动服务应用优化