About MagicVar in Dispatchers
MagicVar is a variable, that will be passed to dispatcher, if dispatcher contain special magic parametr. Name of this parametr can be setted with <magicVarForDispatcher>
parametr, when you initialize server. By default it is "_connection". MagicVar will be passed like Object-Dict (you can access his attributes like <magicVar>[key]
or like <magicVar>.key
).
Example, how it's working by default.
def testDispatcher1(param1, param2, _connection=None):
print 'IP:', _connection.ip
if _connection.ip=='127.0.0.1': return 'Hello, localhost!'
else: return 'Hello, '+_connection.ip
server=flaskJSONRPCServer(("0.0.0.0", 7001))
server.registerFunction(testDispatcher1, path='/api')
server.start()
Example, how to change name of MagicVar.
def testDispatcher1(param1, param2, magicVar=None):
print 'IP:', magicVar.ip
if magicVar.ip=='127.0.0.1': return 'Hello, localhost!'
else: return 'Hello, '+magicVar.ip
server=flaskJSONRPCServer(("0.0.0.0", 7001), magicVarForDispatcher='magicVar')
server.registerFunction(testDispatcher1, path='/api')
server.start()
Object MagicVar, passed to dispatcher, will contain next attributes:
headers | dict | Request's headers. |
cookies | list | Request's cookies. |
ip | str | Request's IP (ip of client). |
headersOut | dict | You can set headers, that will be passed to response. |
cookiesOut | list | You can set cookies, that will be passed to response. |
uniqueId | str | Unique identificator of request. |
jsonp | bool | str | If this requests is JSONP fallback, the output format string passed here, otherwise False. You can change this for JSONP fallback requests. |
mimeType | str | Request's MimeType. You can change this for setting MimeType of Response. |
allowCompress | bool | Is compressing allowed for this request. You can change it for forcing compression. |
server | instance | Link to server's instance. |
serverName | str | Name of server. |
servedBy | set(isRawSocket, (ip | socketPath), (port | socket)) | Info about server's adress. Usefull if you use multiple adresses for one server. |
call | object | Some useful server's methods, you can call them. See this for more info |
nativeThread | bool | Is this request and dispatcher executed in native python thread. |
notify | bool | Is this request is Notify-request. |
dispatcher | func | Link to dispatcher. |
path | str | Server's path, that client used for sending request. Useful if you bind one dispatcher to different paths. |
dispatcherName | str | Name of dispatcher, that passed with request. Useful if you bind one dispatcher to different names. |
parallelType | str | Optional parametr. Can be passed by ExecBackend and contain name of it. |
parallelPoolSize | int | Optional parametr. Can be passed by ExecBackend and contain size of exec's pool. |
parallelId | str | Optional parametr. Can be passed by ExecBackend and contain identificator of process or thread, that processing this request. |
Special attribute call contains some methods, specially prepared for using from dispatchers.
sleep(s, forceNative=False)
¶
Description ¶
This method is wrapper above time.sleep() or gevent.sleep(). Method swithing automatically, if <forceNative> is False. If it's True, always use unpatched time.sleep(). Inherid from this method.
Parametrs ¶
s | float | Delay in seconds. |
forceNative | bool |
log(level, *args)
¶
Description ¶
This method is wrapper for logger. First parametr <level> is optional, if it not setted, message is interpreted as "critical" and will be shown also if logging disabled. Inherid from this method.
Parametrs ¶
level | int | Info-level of message. 0 is critical (and visible always), 1 is error, 2 is warning, 3 is info, 4 is debug. If is not number, it passed as first part of message. |
unlock(exclusive=False)
¶
Description ¶
This method unlocking current dispatcher.
If all server locked, you can unlock specific dispatcher by pass <exclusive> to True. Inherid from this method.
Parametrs ¶
exclusive | bool |
wait(returnStatus=False)
¶
Description ¶
This method wait while server (or specific <dispatcher>) locked or return locking status.
If <returnStatus> is True, method only return locking status.
If <returnStatus> is False, method cyclically call <sleepMethod> until server or <dispatcher> locked. If <sleepMethod> not passed, it will be automatically selected. Inherid from this method.
Parametrs ¶
returnStatus | bool |
Optionally, some execution-backends can add another methods to call attribute.