Python3 notes

简介: Python3 notes

文件下载对话框

我们先在当前目录下创建 foo.txt 文件,用于程序的下载。

文件下载通过设置HTTP头信息来实现,功能代码如下:

实例

#!/usr/bin/python3


# HTTP 头部

print("Content-Disposition: attachment; filename=\"foo.txt\"")

print()

# 打开文件

fo =open("foo.txt","rb")


str= fo.read();

print(str)


# 关闭文件

fo.close()



第一个 cgi 程序这个部分,在英文的 linux 系统下,如果你按照教程的范文编写执行会抛出错误,解决办法有两个,一是修改程序,修改默认stdout,修改后的程序如下:

#!/usr/bin/python3


import sys

import io

sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')


print("Content-type:text/html")

print()                             # 空行,告诉服务器结束头部

print('')

print('')

print('')

print('Hello Word - 我的第一个 CGI 程序!')

print('')

print('')

print('

Hello Word! 我是来自菜鸟教程的第一CGI程序

')

print('')

print('')

或者修改系统的 i18n 亦可。

相关文章
|
5月前
|
Linux Apache Python
Python3 notes
Python3 notes
|
12月前
|
安全 Python
Python3 notes
Python3 notes
|
12月前
|
Python
Python3 notes
Python3 notes
|
12月前
|
Python
Python3 notes
Python3 notes
|
索引 Python
|
存储 数据可视化 API
70个注意的Python小Notes
Python读书笔记:70个注意的小Notes 作者:白宁超 2018年7月9日10:58:18 摘要:在阅读python相关书籍中,对其进行简单的笔记纪要。旨在注意一些细节问题,在今后项目中灵活运用,并对部分小notes进行代码标注。
1321 0
|
Python C语言 .NET
Python chapter 8 learning notes
版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78267298 ...
952 0
|
Python
Python chapter 2&3 learning notes
版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243401 方法是Python对数据执行的操作。
1312 0
|
Python
Python chapter 4 learning notes
版权声明:本文为博主原创文章,原文均发表自http://www.yushuai.me。未经允许,禁止转载。 https://blog.csdn.net/davidcheungchina/article/details/78243661 1.
975 0