2012-02-12 17 views
2

我正在關注this Virtuoso Web Service example。我的POST響應有一個非零長度,但.read()是空的。這隻發生在POST成功時。如果我故意犯了一個錯誤,我會得到一個非零的.read()。POST響應長度非零,但讀取爲空

import httplib 
url = 'lod.openlinksw.com' 

xmlString = '''<?xml version="1.0"?> 
<query xmlns="http://openlinksw.com/services/facets/1.0" inference="" same-as=""> 
    <text>Seattle Mariners traveled all the way to Japan to watch</text> 
    <view type="text" limit="20" offset=""/> 
</query>''' 

xml = open('a.xml','w') 
xml.write(xmlString) 
xml.close() 
xml = open('a.xml') 

headers = {'Content-Type': 'text/xml',} 
conn = httplib.HTTPConnection(url) 
conn.request("POST", "/fct/service", xml, headers) 
re = conn.getresponse() 
conn.close() 

data = re.read() 
print re.reason, re.status, '| len:', re.length, '| read() len:', len(data) 

返回...

OK 200 | len: 19902 | read() len: 0 

如果您有意malform的XML(例如, 「查詢」 >> 「queryzzz」)...

Internal Server Error 500 | len: 0 | read() len: 340 

我相信我只是做一些愚蠢的事情。我的19902字節響應在哪裏?

+0

可能re.length()包含頭的長度? – pycoder112358 2012-02-12 18:55:28

回答

2

變化

conn.close() 

data = re.read() 

data = re.read() 
conn.close() 

您需要關閉連接或任何尚未傳輸的字節將丟失之前讀取數據。