2016-09-05 71 views
0

在此先感谢。我有一个从表单发布数据创建的字典。我想将字典的内容打印到另一个文件。我不想用它输出到一个html页面。打印cgi fieldstorage对象到文件而不是html页面python

def cgiFieldStorageToDict(fieldStorage): 
    # Makes a dictionary from the http post data 
    params = {} 
    for key in fieldStorage.keys(): 
     params[ key ] = fieldStorage[ key ].value 
    return params 



my_dict = cgiFieldStorageToDict(cgi.FieldStorage()) 
s = open('dict_read.py','w') 
z = str(my_dict) 
s.write(z) 

当我打开dict_read.py文件,我只看到一个空的字典:

{} 

我要去哪里错了?

+0

你确定实际上是在查询字符串(如果使用GET)或正文(如果使用POST)中将字段传递给CGI脚本。你如何提交HTTP请求? – mhawke

+0

@mhawke是的,我正在使用发布数据。我想出了一个解决方案 –

回答

0

我无法写入文件,因为它没有适合所有用户写入的权限。我将文件chmod修改为777,其中一些代码现在可以按我想要的方式工作。这里是代码:

form = cgi.FieldStorage() 
latitude = form.getvalue('latitude') 
s = open('any_read.py','w') 
s.write(str(latitude)) 
s.close()