【问题标题】:Web Server using pycurl - GET使用 pycurl 的 Web 服务器 - GET
【发布时间】:2014-10-24 22:14:38
【问题描述】:

我正在使用 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

【问题讨论】:

    标签: get pycurl


    【解决方案1】:

    我刚刚发现问题出在哪里,在服务器端的最后几行代码中应该是:

    self.send_header('Content-type', 'application/json')
    self.end_headers()
    self.wfile.write(rows_json)
    self.wfile.close()
    print rows_json
    return
    

    【讨论】:

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