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]
|
||||
if buffer_address == ffi.NULL:
|
||||
return None
|
||||
return ffi.unpack(buffer_address, length)
|
||||
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||
def get_method(self):
|
||||
buffer = ffi.new("char**")
|
||||
length = lib.uws_req_get_method(self.req, buffer)
|
||||
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||
if buffer_address == ffi.NULL:
|
||||
return None
|
||||
return ffi.unpack(buffer_address, length)
|
||||
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||
def get_header(self, lower_case_header):
|
||||
buffer = ffi.new("char**")
|
||||
data = lower_case_header.encode("utf-8")
|
||||
|
@ -285,7 +285,7 @@ class AppRequest:
|
|||
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||
if buffer_address == ffi.NULL:
|
||||
return None
|
||||
return ffi.unpack(buffer_address, length)
|
||||
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||
def get_query(self, key):
|
||||
buffer = ffi.new("char**")
|
||||
data = key.encode("utf-8")
|
||||
|
@ -293,14 +293,14 @@ class AppRequest:
|
|||
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||
if buffer_address == ffi.NULL:
|
||||
return None
|
||||
return ffi.unpack(buffer_address, length)
|
||||
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||
def get_parameter(self, index):
|
||||
buffer = ffi.new("char**")
|
||||
length = lib.uws_req_get_parameter(self.req, ffi.cast("unsigned short", index), buffer)
|
||||
buffer_address = ffi.addressof(buffer, 0)[0]
|
||||
if buffer_address == ffi.NULL:
|
||||
return None
|
||||
return ffi.unpack(buffer_address, length)
|
||||
return ffi.unpack(buffer_address, length).decode("utf-8")
|
||||
def set_yield(self, has_yield):
|
||||
lib.uws_req_set_field(self.req, 1 if has_yield else 0)
|
||||
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
|
||||
res.run_async(delayed_hello(delay, res))
|
||||
|
||||
async def json(res, _):
|
||||
#req maybe will not be available in direct attached async functions
|
||||
#but if you dont care about req info you can do it
|
||||
async def json(res, req):
|
||||
#request object only lives during the life time of this call
|
||||
#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
|
||||
res.end({ "message": "I'm delayed!"})
|
||||
res.end({ "message": "I'm delayed!", "user-agent": user_agent})
|
||||
|
||||
def not_found(res, req):
|
||||
res.write_status(404).end("Not Found")
|
||||
|
|
|
@ -71,7 +71,9 @@ def list_pokemon(res, req):
|
|||
|
||||
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.get("/", list_original_pokemons)
|
||||
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
|
||||
res.end({ "message": "I'm an application/json!"})
|
||||
|
||||
async def sleepy_json(res, _):
|
||||
#req maybe will not be available in direct attached async functions
|
||||
async def sleepy_json(res, req):
|
||||
#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
|
||||
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):
|
||||
res.write_header("Content-Type", "application/octet-stream")
|
||||
|
|
Ładowanie…
Reference in New Issue