【问题标题】:How to print individual rows from a post header, python如何从帖子标题中打印单个行,python
【发布时间】:2015-11-10 12:11:33
【问题描述】:

这是重要的代码:

elif msgheader.startswith( 'POST' ):
        print "The POST msg:\n", msgheader

这是回应:

The POST msg:
POST / HTTP/1.1
Host: 192.168.1.102:63166
Connection: keep-alive
Content-Length: 12
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
Origin: http://192.168.1.102:63166
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.80 Safari/537.36
Content-Type: application/x-www-form-urlencoded
Referer: http://192.168.1.102:63166/
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.8

comment=test

现在我的问题是如何打印出响应的各个行,例如评论字段。我尝试使用此代码,但出现错误:

print['comment']

Exception (with type 'exceptions.TypeError'): string indices must be integers, not str

【问题讨论】:

    标签: python post http-post


    【解决方案1】:

    假设您使用的是 urllib2,则必须使用类似的东西:

    print msgheader.getheader('content-type')
    

    你应该使用 requests 库:

    print msgheader['content-type']
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多