如何使用python CGI在http请求标头中设置自定义值?

时间:2018-11-10 16:37:27

标签: python python-3.x http-headers cgi

我正在尝试以CGI方式(没有框架)了解python的工作方式。我有一个小的python脚本,可以读取所有http标头并在浏览器上显示。现在,我想学习如何在Request标头中设置新标头。 请帮我怎么做。我能够理解,我可以使用诸如django或flask的框架设置http请求标头。但是,我想以CGI方式尝试。

下面是我的脚本打印标题,

#!/usr/bin/python3
import os


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

# I want to set custom HTTP Request Header Here

def get_http_headers():
    oth_headers = {}
    if True: # config_data.other_headers:
        try:
            for key, value in os.environ.items():
                if key.startswith("HTTP_"):
                    oth_headers[key] = str(value)
        except Exception as e:
                print('Exception in getting headers: {}'.format(e))
    return oth_headers


oth = get_http_headers()
print(oth)

帮助我如何在请求标头中添加自定义标头。预先感谢。

0 个答案:

没有答案
相关问题