0

I'm setting up a webserver using pycurl, and im having a small problem, where if i send commands using CURL, i get the json response, but if i use the pycurl method i dont get a response from the server, i have check the packets flow using wireshark, the packets are in fact arriving to the server, but the server is not responding back.

Client Side code:

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

Server Side code:

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
Dave Fontes
  • 19
  • 1
  • 1
  • 5

1 Answers1

0

i just found out what the problem is, on the last lines of code on the server side it should be:

self.send_header('Content-type', 'application/json')
self.end_headers()
self.wfile.write(rows_json)
self.wfile.close()
print rows_json
return
Dave Fontes
  • 19
  • 1
  • 1
  • 5