【问题标题】:Python: how to send html-code in a POST requestPython:如何在 POST 请求中发送 html 代码
【发布时间】:2014-08-16 08:07:15
【问题描述】:

我尝试将 htm 代码从 python 脚本发送到 Joomla 站点。

description = "<h1>Header</h1><p>text</p>"   

values = {'description' : description.encode(self.encoding),
          'id = '       : 5,
         }
data = urlencode(values)
binData = data.encode(self.encoding)

headers = { 'User-Agent' : self.userAgent,
            'X-Requested-With' : 'XMLHttpRequest'}

req = urllib2.Request(self.addr, binData, headers)

response = urllib2.urlopen(req)
rawreply = response.read()

在 Joomla-server 我得到了相同的字符串,但没有 html:

$desc       = JRequest::getVar('description', '', 'POST');

怎么了?

【问题讨论】:

    标签: python joomla http-headers urllib2 urllib


    【解决方案1】:

    你应该使用请求

    pip install requests
    

    然后

    import requests
    
    description = "<h1>Header</h1><p>text</p>"   
    values = dict(description='description',id=5)
    response = requests.post(self.addr,data=values)
    
    if response.ok:
        print response.json()
    

    或者如果 joomla 没有返回 json

    print response.content
    

    【讨论】:

    • 我很有趣。几乎所有示例都使用 urllib。谢谢!
    【解决方案2】:

    JRequest::getVar 或 JRequest::getString 过滤 HTML 代码。但可以关闭:

    $desc = JRequest::getVar('description', '', 'POST', 'string', JREQUEST_ALLOWHTML);
    

    【讨论】:

      猜你喜欢
      • 2017-11-17
      • 2022-11-03
      • 1970-01-01
      • 2022-12-06
      • 1970-01-01
      • 2012-07-04
      • 2021-01-09
      • 2015-10-23
      • 1970-01-01
      相关资源
      最近更新 更多