简体   繁体   中英

Reading a client's header from Python CGI script?

I'm writing a very simple web service, written in Python and run as CGI on an Apache server.

According to Python docs (somewhere... I forgot where), I can use sys.stdin to read the data POSTed by a random client, and this has been working fine. However, I would like to be able to read the HTTP header information as well - incoming IP, user agent, and so on. I'd also like to keep it very simple for now, by using only Python libraries (so no mod-python). How do I do this?

If you are running as a CGI, you can't read the HTTP header directly, but the web server put much of that information into environment variables for you. You can just pick it out of os.environ[]

The list of environment variables that might be there is pretty long. You can find it by doing a web search for "common gateway interface". For example, in http://www.ietf.org/rfc/rfc3875.txt they are called "meta-variables".

These are given to the CGI script through the environment:

import os
user_agent = os.environ["HTTP_USER_AGENT"]
ip = os.environ["REMOTE_ADDR"]

As this page explains, most HTTP request headers are made available to your CGI script via environment variables. Run cgi.test() instead of your script to see the environment (including HTTP request headers) shown back to your visiting browser.

This is how I capture in Python 3 from CGI (A) URL, (B) GET parameters and (C) POST data:

=======================================================

import sys, os, io

CAPTURE URL

myDomainSelf = os.environ.get('SERVER_NAME')

myPathSelf = os.environ.get('PATH_INFO')

myURLSelf = myDomainSelf + myPathSelf

CAPTURE GET DATA

myQuerySelf = os.environ.get('QUERY_STRING')

CAPTURE POST DATA

myTotalBytesStr=(os.environ.get('HTTP_CONTENT_LENGTH'))

if (myTotalBytesStr == None):

myJSONStr = '{"error": {"value": true, "message": "No (post) data received"}}'

else:

myTotalBytes=int(os.environ.get('HTTP_CONTENT_LENGTH'))

myPostDataRaw = io.open(sys.stdin.fileno(),"rb").read(myTotalBytes)

myPostData = myPostDataRaw.decode("utf-8")

Write RAW to FILE

mySpy = "myURLSelf: [" + str(myURLSelf) + "]\\n"

mySpy = mySpy + "myQuerySelf: [" + str(myQuerySelf) + "]\\n"

mySpy = mySpy + "myPostData: [" + str(myPostData) + "]\\n"

You need to define your own myPath here

myFilename = "spy.txt"

myFilePath = myPath + "\\" + myFilename

myFile = open(myFilePath, "w")

myFile.write(mySpy)

myFile.close()

=======================================================

Here are some other useful CGI environment vars:

AUTH_TYPE

CONTENT_LENGTH

CONTENT_TYPE

GATEWAY_INTERFACE

PATH_INFO

PATH_TRANSLATED

QUERY_STRING

REMOTE_ADDR

REMOTE_HOST

REMOTE_IDENT

REMOTE_USER

REQUEST_METHOD

SCRIPT_NAME

SERVER_NAME

SERVER_PORT

SERVER_PROTOCOL

SERVER_SOFTWARE

=================================

I am using this methods on Windows Server with MIIS and Python 3 in CGI mode.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM