python cgi脚本不会使用lxml etree write方法写入xml文件

2024-10-01 07:35:28 发布

您现在位置:Python中文网/ 问答频道 /正文

我对编写cgi脚本还不熟悉。我编写了一个简单的cgi脚本,它应该基于查询字符串将新记录写入xml文件,或者读写它。当我尝试读取它时,它工作得很好,但它无法写入xml文件。我的代码如下:

#!/usr/bin/python3
import cgi
from lxml import etree

form = cgi.FieldStorage()
option = form.getvalue("option", "(no data)")

print("Content-Type: application/xml")
print("")

#xmldom = etree.parse('../lab7/zadanie_ajax/opcje.xml')
xmldom = etree.parse('/home/stud2018/8mikolajczyk/public_html/lab7/zadanie_ajax/opcje.xml')

if (option == "add"):
    child = etree.Element("record") 
    title_text = form.getvalue("title", "(no data)")
    author_text = form.getvalue("author", "(no data)")
    title = etree.SubElement(child, "title")
    title.text = title_text
    author = etree.SubElement(child, "author")
    author.text = author_text

    xmlroot = xmldom.getroot()
    xmlroot.append(child)
    xmldom.write('/home/stud2018/8mikolajczyk/public_html/lab7/zadanie_ajax/opcje.xml')


if (option == "get"):
    print(etree.tostring(xmldom, pretty_print=True, encoding="unicode"))

我尝试按以下方式运行脚本:

/cgi-bin/zad6ajax.py?option=add&title=q&author=w

这样运行之后,我收到以下错误:

--&燃气轮机;
<body bgcolor="#f0f0f8"><font color="#f0f0f8" size="-5"> --> -->
</font> </font> </font> </script> </object> </blockquote> </pre>
</table> </table> </table> </table> </table> </font> </font> </font><pre>Traceback (most recent call last):
  File "/home/stud2018/8mikolajczyk/public_html/cgi-bin/zad6ajax.py", line 28, in &lt;module&gt;
    xmldom.write('opcje.xml')
  File "src/lxml/etree.pyx", line 2057, in lxml.etree._ElementTree.write
  File "src/lxml/serializer.pxi", line 746, in lxml.etree._tofilelike
  File "src/lxml/serializer.pxi", line 813, in lxml.etree._create_output_buffer
  File "src/lxml/serializer.pxi", line 803, in lxml.etree._create_output_buffer
PermissionError: [Errno 13] Permission denied
</pre>

Tags: textinformtitlelinetablexmllxml