Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/python-2.7/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用python执行HTTP重定向_Python_Python 2.7_Redirect_Cgi - Fatal编程技术网

如何使用python执行HTTP重定向

如何使用python执行HTTP重定向,python,python-2.7,redirect,cgi,Python,Python 2.7,Redirect,Cgi,我在用户提交HTML表单后调用python脚本。我的python脚本位于usr/lib/cgi-bin中。我想根据用户提供的输入重定向到另一个页面,基本上我正在创建一个登录脚本,如果用户输入有效,那么用户将被重定向到loggedin.html页面,或者重定向到error.html页面 signin.py(我正在尝试执行此代码) #/usr/bin/python2.7 导入cgi,cgitb #导入pymongo模块以连接到mongodb数据库 进口pymongo 从pymongo导入MongoC

我在用户提交HTML表单后调用python脚本。我的python脚本位于usr/lib/cgi-bin中。我想根据用户提供的输入重定向到另一个页面,基本上我正在创建一个登录脚本,如果用户输入有效,那么用户将被重定向到loggedin.html页面,或者重定向到error.html页面

signin.py(我正在尝试执行此代码)

#/usr/bin/python2.7
导入cgi,cgitb
#导入pymongo模块以连接到mongodb数据库
进口pymongo
从pymongo导入MongoClient
#创建FieldStorage的实例
form=cgi.FieldStorage()
#为正在运行的mongod实例创建mongo客户端
client=MongoClient()
#选择数据库mydb
db_mydb=client.mydb
#选择集合用户
集合\u user=db\u mydb.user
打印“内容类型:text/html\r\n\r\n”
打印“”
打印“”
打印“签名”
打印“”
#从字段中获取数据
email=form.getvalue('login-username')
password=form.getvalue('login-password')
#检查用户输入是否正确
存在性查询=集合用户。查找一个({“\u id”:电子邮件,“密码”:密码})
打印“”
如果(存在查询):
打印“人存在”
打印“状态:301已移动\r\n\r\n”
“打印”位置:http://localhost/mongo/loggedin.html\r\n\r\n“
其他:
打印“人不存在”
#重定向到error.html页面
打印“”
打印“”
从html表单调用此脚本后得到的输出(当用户输入有效时,即用户是有效的):

人员存在位置:http:localhost/mongo/loggedin.html


我不熟悉使用python的cgi,因此从
cgi
模块上的python文档中获得的任何帮助对我都是非常好的

CGI脚本的输出应该由两个部分组成,由一个空行分隔。第一部分包含许多标题,告诉客户机下面是什么类型的数据

您必须在发送任何其他数据之前发送标题
。为了实现您的目标,只需将所有头发送逻辑放在HTML发送逻辑之前

但是您最好将所有表单检查代码移到另一个文件中,这样它就不会打印任何内容,只发送重定向和其他内容

 # Import modules for CGI handling 

    import cgi, cgitb 

    # import pymongo module for connecting to mongodb database
    import pymongo
    from pymongo import MongoClient

    # Create instance of FieldStorage 
    form = cgi.FieldStorage() 

    # creating a mongo client to the running mongod instance
    # The code will connect on the default host and port i.e 'localhost' and '27017'
    client = MongoClient()

    # selecting the database mydb 
    db_mydb = client.mydb

    # selecting the collection user
    collection_user = db_mydb.user

    #print "Content-type:text/html\r\n\r\n"

    # Get data from fields
    email = form.getvalue('login-username')
    password  =     form.getvalue('login-password')

    #checking whether user inputs are correct or not
    existence_query = collection_user.find_one({"_id":email,"password":password})


    if(existence_query):
        print "Location:http://localhost/mongo/index.html\r\n"
        print "Content-type:text/html\r\n\r\n"
    else:
        print "Location:http://localhost/mongo/index.html\r\n"
        print "Content-type:text/html\r\n\r\n"
在将任何其他数据发送到浏览器之前,需要在开始时初始化标题。在问题中,打印语句(如print“
)是在位置标题之前写入的,因此未达到预期结果

 # Import modules for CGI handling 

    import cgi, cgitb 

    # import pymongo module for connecting to mongodb database
    import pymongo
    from pymongo import MongoClient

    # Create instance of FieldStorage 
    form = cgi.FieldStorage() 

    # creating a mongo client to the running mongod instance
    # The code will connect on the default host and port i.e 'localhost' and '27017'
    client = MongoClient()

    # selecting the database mydb 
    db_mydb = client.mydb

    # selecting the collection user
    collection_user = db_mydb.user

    #print "Content-type:text/html\r\n\r\n"

    # Get data from fields
    email = form.getvalue('login-username')
    password  =     form.getvalue('login-password')

    #checking whether user inputs are correct or not
    existence_query = collection_user.find_one({"_id":email,"password":password})


    if(existence_query):
        print "Location:http://localhost/mongo/index.html\r\n"
        print "Content-type:text/html\r\n\r\n"
    else:
        print "Location:http://localhost/mongo/index.html\r\n"
        print "Content-type:text/html\r\n\r\n"