kopia lustrzana https://github.com/cirospaciari/socketify.py
added more info about async and decoded to utf8 string results from request
rodzic
47880cf9e9
commit
8a7333a4bd
|
@ -270,14 +270,14 @@ class AppRequest:
|
||||||
buffer_address = ffi.addressof(buffer, 0)[0]
|
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||||
if buffer_address == ffi.NULL:
|
if buffer_address == ffi.NULL:
|
||||||
return None
|
return None
|
||||||
return ffi.unpack(buffer_address, length)
|
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||||
def get_method(self):
|
def get_method(self):
|
||||||
buffer = ffi.new("char**")
|
buffer = ffi.new("char**")
|
||||||
length = lib.uws_req_get_method(self.req, buffer)
|
length = lib.uws_req_get_method(self.req, buffer)
|
||||||
buffer_address = ffi.addressof(buffer, 0)[0]
|
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||||
if buffer_address == ffi.NULL:
|
if buffer_address == ffi.NULL:
|
||||||
return None
|
return None
|
||||||
return ffi.unpack(buffer_address, length)
|
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||||
def get_header(self, lower_case_header):
|
def get_header(self, lower_case_header):
|
||||||
buffer = ffi.new("char**")
|
buffer = ffi.new("char**")
|
||||||
data = lower_case_header.encode("utf-8")
|
data = lower_case_header.encode("utf-8")
|
||||||
|
@ -285,7 +285,7 @@ class AppRequest:
|
||||||
buffer_address = ffi.addressof(buffer, 0)[0]
|
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||||
if buffer_address == ffi.NULL:
|
if buffer_address == ffi.NULL:
|
||||||
return None
|
return None
|
||||||
return ffi.unpack(buffer_address, length)
|
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||||
def get_query(self, key):
|
def get_query(self, key):
|
||||||
buffer = ffi.new("char**")
|
buffer = ffi.new("char**")
|
||||||
data = key.encode("utf-8")
|
data = key.encode("utf-8")
|
||||||
|
@ -293,14 +293,14 @@ class AppRequest:
|
||||||
buffer_address = ffi.addressof(buffer, 0)[0]
|
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||||
if buffer_address == ffi.NULL:
|
if buffer_address == ffi.NULL:
|
||||||
return None
|
return None
|
||||||
return ffi.unpack(buffer_address, length)
|
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||||
def get_parameter(self, index):
|
def get_parameter(self, index):
|
||||||
buffer = ffi.new("char**")
|
buffer = ffi.new("char**")
|
||||||
length = lib.uws_req_get_parameter(self.req, ffi.cast("unsigned short", index), buffer)
|
length = lib.uws_req_get_parameter(self.req, ffi.cast("unsigned short", index), buffer)
|
||||||
buffer_address = ffi.addressof(buffer, 0)[0]
|
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||||
if buffer_address == ffi.NULL:
|
if buffer_address == ffi.NULL:
|
||||||
return None
|
return None
|
||||||
return ffi.unpack(buffer_address, length)
|
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||||
def set_yield(self, has_yield):
|
def set_yield(self, has_yield):
|
||||||
lib.uws_req_set_field(self.req, 1 if has_yield else 0)
|
lib.uws_req_set_field(self.req, 1 if has_yield else 0)
|
||||||
def get_yield(self):
|
def get_yield(self):
|
||||||
|
|
|
@ -16,11 +16,13 @@ def home(res, req):
|
||||||
#abort handler is grabed here, so responses only will be send if res.aborted == False
|
#abort handler is grabed here, so responses only will be send if res.aborted == False
|
||||||
res.run_async(delayed_hello(delay, res))
|
res.run_async(delayed_hello(delay, res))
|
||||||
|
|
||||||
async def json(res, _):
|
async def json(res, req):
|
||||||
#req maybe will not be available in direct attached async functions
|
#request object only lives during the life time of this call
|
||||||
#but if you dont care about req info you can do it
|
#get parameters, query, headers anything you need here before first await :)
|
||||||
|
user_agent = req.get_header("user-agent")
|
||||||
|
#req maybe will not be available in direct attached async functions after await
|
||||||
await asyncio.sleep(2) #do something async
|
await asyncio.sleep(2) #do something async
|
||||||
res.end({ "message": "I'm delayed!"})
|
res.end({ "message": "I'm delayed!", "user-agent": user_agent})
|
||||||
|
|
||||||
def not_found(res, req):
|
def not_found(res, req):
|
||||||
res.write_status(404).end("Not Found")
|
res.write_status(404).end("Not Found")
|
||||||
|
|
|
@ -71,7 +71,9 @@ def list_pokemon(res, req):
|
||||||
|
|
||||||
res.run_async(find_pokemon(number, res))
|
res.run_async(find_pokemon(number, res))
|
||||||
|
|
||||||
|
###
|
||||||
|
# Here i decided to use an sync first and async only if needs, but you can use async directly see ./async.py
|
||||||
|
###
|
||||||
app = App()
|
app = App()
|
||||||
app.get("/", list_original_pokemons)
|
app.get("/", list_original_pokemons)
|
||||||
app.get("/:number", list_pokemon)
|
app.get("/:number", list_pokemon)
|
||||||
|
|
|
@ -38,11 +38,13 @@ def json(res, req):
|
||||||
#if you pass an object will auto write an header with application/json
|
#if you pass an object will auto write an header with application/json
|
||||||
res.end({ "message": "I'm an application/json!"})
|
res.end({ "message": "I'm an application/json!"})
|
||||||
|
|
||||||
async def sleepy_json(res, _):
|
async def sleepy_json(res, req):
|
||||||
#req maybe will not be available in direct attached async functions
|
#get parameters, query, headers anything you need here before first await :)
|
||||||
|
user_agent = req.get_header("user-agent")
|
||||||
|
#req maybe will not be available in direct attached async functions after await
|
||||||
#but if you dont care about req info you can do it
|
#but if you dont care about req info you can do it
|
||||||
await asyncio.sleep(2) #do something async
|
await asyncio.sleep(2) #do something async
|
||||||
res.end({ "message": "I'm delayed!"})
|
res.end({ "message": "I'm delayed!", "user-agent": user_agent})
|
||||||
|
|
||||||
def custom_header(res, req):
|
def custom_header(res, req):
|
||||||
res.write_header("Content-Type", "application/octet-stream")
|
res.write_header("Content-Type", "application/octet-stream")
|
||||||
|
|
Ładowanie…
Reference in New Issue