如何从帖子标题中打印单个行,python

时间:2015-11-10 12:11:33

标签: python post http-post

这是重要的代码:

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

1 个答案:

答案 0 :(得分:0)

你必须使用这样的东西,假设你正在使用urllib2:

print msgheader.getheader('content-type')

对于lib请求,您应该使用:

print msgheader['content-type']