我正在使用pycurl设置网络服务器,我有一个小问题,如果我使用CURL发送命令,我得到json响应,但如果我使用pycurl方法我没有得到响应服务器,我已经使用wireshark检查数据包流,数据包实际上是到达服务器,但服务器没有响应。
客户端代码:
def listUsers(args):
url = 'localhost:7070'
storage = StringIO()
c = pycurl.Curl()
c.setopt(c.URL, url)
c.setopt(c.WRITEFUNCTION, storage.write)
c.perform()
c.close()
content = storage.getvalue()
print content
服务器端代码:
def do_GET(self):
print(self.headers)
tmp, user = os.path.split(self.path)
if tmp != '/':
if c.execute('select * from user where username = ?', (user, )).fetchone() is None:
self.send_response(404, 'User %s not found' %user)
self.end_headers()
return
else:
c.execute('select * from user')
recs = c.fetchall()
data = []
for rec in recs:
data.append({"username": rec[0]})
rows_json = json.dumps(data)
self.send_response(200)
self.send_header('content-type', 'application/json')
self.wfile.write(rows_json)
self.end_headers()
self.wfile.close()
print rows_json
return
答案 0 :(得分:0)
我刚刚发现了问题所在,在服务器端的最后几行代码应该是:
self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(rows_json)
self.wfile.close()
print rows_json
return