librelist archives

« back to archive

Datetime of the Request

Datetime of the Request

From:
Alberto Megia
Date:
2013-09-19 @ 14:22
Hi! first email in Flask's list :)

So I was trying to get request's date and inspecting flask.request I have
seen "date" attribute, but seems like is not working, at least for me

With this little example:


from flask import Flask, request

app = Flask(__name__)

@app.route('/')
def hello():
    print request.date

    return "Hey Flask people"

if __name__ == '__main__':
    app.run()


I get:  None

Can I get request's date from Flask objects?

Thanks a lot,
Alberto

Re: [flask] Datetime of the Request

From:
Patrice Neff
Date:
2013-09-19 @ 15:35
Hi Alberto

So I was trying to get request's date and inspecting flask.request I have
> seen "date" attribute, but seems like is not working, at least for me
>

This field gives access to the `Date` field from the request headers. The
HTTP specification has this to say about that: "Clients SHOULD only send a
Date header field in messages that include an entity-body, as in the case
of the PUT and POST requests, and even then it is optional. A client
without a clock MUST NOT send a Date header field in a request."

I'm not sure if any browsers include that field.

 Can I get request's date from Flask objects?
>

Which date exactly do you need? If you simply need the current date I'd use
one of Python's functions, such as `datetime.datetime.utcnow()`. For
long-running requests, Gary's answer shows how you can combine this with
making sure you get the date from when the request actually came in.

Patrice

Re: [flask] Datetime of the Request

From:
Shiv Shankar Dayal
Date:
2013-09-19 @ 15:46
Hi,

Sorry for intruding but I believe Patrice's solution of
`datetime.datetime.utcnow()` to be best because the time difference between
request and response is probably few hundreds of milliseconds in worst
case. Also, date of client may be different than date of server. I am not
sure what the usage of request's date here is. Unless the semantics of date
is clear it is rather absurd trying to propose a solution. The question I
ask is why one needs request date? Is it client's date or server's date?


On Thu, Sep 19, 2013 at 9:05 PM, Patrice Neff <mailinglists@patrice.ch>wrote:

> Hi Alberto
>
> So I was trying to get request's date and inspecting flask.request I have
>> seen "date" attribute, but seems like is not working, at least for me
>>
>
> This field gives access to the `Date` field from the request headers. The
> HTTP specification has this to say about that: "Clients SHOULD only send a
> Date header field in messages that include an entity-body, as in the case
> of the PUT and POST requests, and even then it is optional. A client
> without a clock MUST NOT send a Date header field in a request."
>
> I'm not sure if any browsers include that field.
>
>  Can I get request's date from Flask objects?
>>
>
> Which date exactly do you need? If you simply need the current date I'd
> use one of Python's functions, such as `datetime.datetime.utcnow()`. For
> long-running requests, Gary's answer shows how you can combine this with
> making sure you get the date from when the request actually came in.
>
> Patrice
>



-- 
Respect,
Shiv Shankar Dayal

Re: [flask] Datetime of the Request

From:
Alberto Megia
Date:
2013-09-20 @ 08:34
Hi,

First of all, thanks for this quick responses :)

Second, I think I am going to tell you my scenario and you tell me what you
think.

So I was developing a RESTful web service with Flask (Nginx, Gunicorn,
Flask) and came across with this issue: I want every Gunicorn worker log to
the same file. I found that Python does not allow multiprocess log to the
same file, so we decided to use a Queue system (Amazon SQS) to enqueue log
records and only one consumer process will manage that.

So my web service is designed like this: Flask Routing -> Validation ->
Business Logic -> response & log.

I would like to log the datetime when Flask response object was created by
Flask app, and maybe the time when my "log records producer"  enqueues in
SQS (just to watch performance)


@Gary Chambers, if I introduce such decorator, do you think perfromance
would be affected?

@Patrice Neff, "datetime.datetime.utcnow()" would be usefull to get logging
datetime, but request object creation? or the datetime when App Flask
receives the request and starts to process it.

@Shiv Shankar Dayal, my business logic process may spend more than a few
hundreds of milliseconds; it is a data model synchronization

Thx a lot people, it is a pleasure to talk with you
Alberto




2013/9/19 Shiv Shankar Dayal <shivshankar.dayal@gmail.com>

> Hi,
>
> Sorry for intruding but I believe Patrice's solution of
> `datetime.datetime.utcnow()` to be best because the time difference between
> request and response is probably few hundreds of milliseconds in worst
> case. Also, date of client may be different than date of server. I am not
> sure what the usage of request's date here is. Unless the semantics of date
> is clear it is rather absurd trying to propose a solution. The question I
> ask is why one needs request date? Is it client's date or server's date?
>
>
> On Thu, Sep 19, 2013 at 9:05 PM, Patrice Neff <mailinglists@patrice.ch>wrote:
>
>> Hi Alberto
>>
>>  So I was trying to get request's date and inspecting flask.request I
>>> have seen "date" attribute, but seems like is not working, at least for me
>>>
>>
>> This field gives access to the `Date` field from the request headers. The
>> HTTP specification has this to say about that: "Clients SHOULD only send a
>> Date header field in messages that include an entity-body, as in the case
>> of the PUT and POST requests, and even then it is optional. A client
>> without a clock MUST NOT send a Date header field in a request."
>>
>> I'm not sure if any browsers include that field.
>>
>>  Can I get request's date from Flask objects?
>>>
>>
>> Which date exactly do you need? If you simply need the current date I'd
>> use one of Python's functions, such as `datetime.datetime.utcnow()`. For
>> long-running requests, Gary's answer shows how you can combine this with
>> making sure you get the date from when the request actually came in.
>>
>> Patrice
>>
>
>
>
> --
> Respect,
> Shiv Shankar Dayal
>

Re: [flask] Datetime of the Request

From:
Pierre Aubert
Date:
2013-09-20 @ 12:42
Hello,

you can also use a dedicated system like sentry. It is a opensoure system.
It is easy to interface your flask application with sentry via raven.

PIerre





>________________________________
> De : Alberto Megia <amegia@savemeeting.com>
>À : flask@librelist.com 
>Envoyé le : Vendredi 20 septembre 2013 10h34
>Objet : Re: [flask] Datetime of the Request
> 
>
>
>Hi,
>
>First of all, thanks for this quick responses :)
>
>Second, I think I am going to tell you my scenario and you tell me what 
you think.
>
>So I was developing a RESTful web service with Flask (Nginx, Gunicorn, 
Flask) and came across with this issue: I want every Gunicorn worker log 
to the same file. I found that Python does not allow multiprocess log to 
the same file, so we decided to use a Queue system (Amazon SQS) to enqueue
log records and only one consumer process will manage that.
>
>So my web service is designed like this: Flask Routing -> Validation -> 
Business Logic -> response & log.
>
>I would like to log the datetime when Flask response object was created 
by Flask app, and maybe the time when my "log records producer"  enqueues 
in SQS (just to watch performance)
>
>
>@Gary Chambers, if I introduce such decorator, do you think perfromance 
would be affected?
>
>@Patrice Neff, "datetime.datetime.utcnow()" would be usefull to get 
logging datetime, but request object creation? or the datetime when App 
Flask receives the request and starts to process it.
>
>@Shiv Shankar Dayal, my business logic process may spend more than a few 
hundreds of milliseconds; it is a data model synchronization
>
>Thx a lot people, it is a pleasure to talk with you
>Alberto
>
>
>
>
>
>
>
>2013/9/19 Shiv Shankar Dayal <shivshankar.dayal@gmail.com>
>
>Hi,
>>
>>Sorry for intruding but I believe Patrice's solution of 
`datetime.datetime.utcnow()` to be best because the time difference 
between request and response is probably few hundreds of milliseconds in 
worst case. Also, date of client may be different than date of server. I 
am not sure what the usage of request's date here is. Unless the semantics
of date is clear it is rather absurd trying to propose a solution. The 
question I ask is why one needs request date? Is it client's date or 
server's date?
>>
>>
>>
>>
>>On Thu, Sep 19, 2013 at 9:05 PM, Patrice Neff <mailinglists@patrice.ch> wrote:
>>
>>Hi Alberto
>>>
>>>
>>>So I was trying to get request's date and inspecting flask.request I 
have seen "date" attribute, but seems like is not working, at least for me
>>>>
>>>
>>>
>>>This field gives access to the `Date` field from the request headers. 
The HTTP specification has this to say about that: "Clients SHOULD only 
send a Date header field in messages that include an entity-body, as in 
the case of the PUT and POST requests, and even then it is optional. A 
client without a clock MUST NOT send a Date header field in a request."
>>>
>>>
>>>I'm not sure if any browsers include that field.
>>>
>>>
>>>Can I get request's date from Flask objects?
>>>>
>>>
>>>Which date exactly do you need? If you simply need the current date I'd
use one of Python's functions, such as `datetime.datetime.utcnow()`. For 
long-running requests, Gary's answer shows how you can combine this with 
making sure you get the date from when the request actually came in.
>>>
>>>Patrice
>>
>>
>>-- 
>>
>>Respect,
>>
>>Shiv Shankar Dayal
>
>
>

Re: [flask] Datetime of the Request

From:
Alberto Megia
Date:
2013-09-20 @ 13:03
Hi!

Yes Pierre, we can use it but we are first trying with Amazon SQS. A
dedicated server will be an option in the future.
I read this answer -> http://stackoverflow.com/a/9993857/1171280

Thx!




2013/9/20 Pierre Aubert <pierreaubert@yahoo.fr>

> Hello,
>
> you can also use a dedicated system like sentry. It is a opensoure system.
> It is easy to interface your flask application with sentry via raven.
>
> PIerre
>
>
>   ------------------------------
>  *De :* Alberto Megia <amegia@savemeeting.com>
> *À :* flask@librelist.com
> *Envoyé le :* Vendredi 20 septembre 2013 10h34
> *Objet :* Re: [flask] Datetime of the Request
>
> Hi,
>
> First of all, thanks for this quick responses :)
>
> Second, I think I am going to tell you my scenario and you tell me what
> you think.
>
> So I was developing a RESTful web service with Flask (Nginx, Gunicorn,
> Flask) and came across with this issue: I want every Gunicorn worker log to
> the same file. I found that Python does not allow multiprocess log to the
> same file, so we decided to use a Queue system (Amazon SQS) to enqueue log
> records and only one consumer process will manage that.
>
> So my web service is designed like this: Flask Routing -> Validation ->
> Business Logic -> response & log.
>
> I would like to log the datetime when Flask response object was created by
> Flask app, and maybe the time when my "log records producer"  enqueues in
> SQS (just to watch performance)
>
>
> @Gary Chambers, if I introduce such decorator, do you think perfromance
> would be affected?
>
> @Patrice Neff, "datetime.datetime.utcnow()" would be usefull to get
> logging datetime, but request object creation? or the datetime when App
> Flask receives the request and starts to process it.
>
> @Shiv Shankar Dayal, my business logic process may spend more than a few
> hundreds of milliseconds; it is a data model synchronization
>
> Thx a lot people, it is a pleasure to talk with you
> Alberto
>
>
>
>
> 2013/9/19 Shiv Shankar Dayal <shivshankar.dayal@gmail.com>
>
> Hi,
>
> Sorry for intruding but I believe Patrice's solution of
> `datetime.datetime.utcnow()` to be best because the time difference between
> request and response is probably few hundreds of milliseconds in worst
> case. Also, date of client may be different than date of server. I am not
> sure what the usage of request's date here is. Unless the semantics of date
> is clear it is rather absurd trying to propose a solution. The question I
> ask is why one needs request date? Is it client's date or server's date?
>
>
> On Thu, Sep 19, 2013 at 9:05 PM, Patrice Neff <mailinglists@patrice.ch>wrote:
>
> Hi Alberto
>
>  So I was trying to get request's date and inspecting flask.request I
> have seen "date" attribute, but seems like is not working, at least for me
>
>
> This field gives access to the `Date` field from the request headers. The
> HTTP specification has this to say about that: "Clients SHOULD only send a
> Date header field in messages that include an entity-body, as in the case
> of the PUT and POST requests, and even then it is optional. A client
> without a clock MUST NOT send a Date header field in a request."
>
> I'm not sure if any browsers include that field.
>
>  Can I get request's date from Flask objects?
>
>
> Which date exactly do you need? If you simply need the current date I'd
> use one of Python's functions, such as `datetime.datetime.utcnow()`. For
> long-running requests, Gary's answer shows how you can combine this with
> making sure you get the date from when the request actually came in.
>
> Patrice
>
>
>
>
> --
> Respect,
> Shiv Shankar Dayal
>
>
>
>
>

Re: [flask] Datetime of the Request

From:
Shiv Shankar Dayal
Date:
2013-09-20 @ 13:13
"I would like to log the datetime when Flask response object was created by
Flask app, and maybe the time when my "log records producer"  enqueues in
SQS (just to watch performance)"

Since this is what you want as Patrice said you can get the time of server
at that particular point just before response is returned and also
similarly in other case. Now again since you have multiple processes and
you would need to store this information the same mechanism can be used as
log files. :-)


On Fri, Sep 20, 2013 at 6:33 PM, Alberto Megia <amegia@savemeeting.com>wrote:

> Hi!
>
> Yes Pierre, we can use it but we are first trying with Amazon SQS. A
> dedicated server will be an option in the future.
> I read this answer -> http://stackoverflow.com/a/9993857/1171280
>
> Thx!
>
>
>
>
> 2013/9/20 Pierre Aubert <pierreaubert@yahoo.fr>
>
>> Hello,
>>
>> you can also use a dedicated system like sentry. It is a opensoure
>> system. It is easy to interface your flask application with sentry via
>> raven.
>>
>> PIerre
>>
>>
>>   ------------------------------
>>  *De :* Alberto Megia <amegia@savemeeting.com>
>> *À :* flask@librelist.com
>> *Envoyé le :* Vendredi 20 septembre 2013 10h34
>> *Objet :* Re: [flask] Datetime of the Request
>>
>> Hi,
>>
>> First of all, thanks for this quick responses :)
>>
>> Second, I think I am going to tell you my scenario and you tell me what
>> you think.
>>
>> So I was developing a RESTful web service with Flask (Nginx, Gunicorn,
>> Flask) and came across with this issue: I want every Gunicorn worker log to
>> the same file. I found that Python does not allow multiprocess log to the
>> same file, so we decided to use a Queue system (Amazon SQS) to enqueue log
>> records and only one consumer process will manage that.
>>
>> So my web service is designed like this: Flask Routing -> Validation ->
>> Business Logic -> response & log.
>>
>> I would like to log the datetime when Flask response object was created
>> by Flask app, and maybe the time when my "log records producer"  enqueues
>> in SQS (just to watch performance)
>>
>>
>> @Gary Chambers, if I introduce such decorator, do you think perfromance
>> would be affected?
>>
>> @Patrice Neff, "datetime.datetime.utcnow()" would be usefull to get
>> logging datetime, but request object creation? or the datetime when App
>> Flask receives the request and starts to process it.
>>
>> @Shiv Shankar Dayal, my business logic process may spend more than a few
>> hundreds of milliseconds; it is a data model synchronization
>>
>> Thx a lot people, it is a pleasure to talk with you
>> Alberto
>>
>>
>>
>>
>> 2013/9/19 Shiv Shankar Dayal <shivshankar.dayal@gmail.com>
>>
>> Hi,
>>
>> Sorry for intruding but I believe Patrice's solution of
>> `datetime.datetime.utcnow()` to be best because the time difference between
>> request and response is probably few hundreds of milliseconds in worst
>> case. Also, date of client may be different than date of server. I am not
>> sure what the usage of request's date here is. Unless the semantics of date
>> is clear it is rather absurd trying to propose a solution. The question I
>> ask is why one needs request date? Is it client's date or server's date?
>>
>>
>> On Thu, Sep 19, 2013 at 9:05 PM, Patrice Neff <mailinglists@patrice.ch>wrote:
>>
>> Hi Alberto
>>
>>  So I was trying to get request's date and inspecting flask.request I
>> have seen "date" attribute, but seems like is not working, at least for me
>>
>>
>> This field gives access to the `Date` field from the request headers. The
>> HTTP specification has this to say about that: "Clients SHOULD only send a
>> Date header field in messages that include an entity-body, as in the case
>> of the PUT and POST requests, and even then it is optional. A client
>> without a clock MUST NOT send a Date header field in a request."
>>
>> I'm not sure if any browsers include that field.
>>
>>  Can I get request's date from Flask objects?
>>
>>
>> Which date exactly do you need? If you simply need the current date I'd
>> use one of Python's functions, such as `datetime.datetime.utcnow()`. For
>> long-running requests, Gary's answer shows how you can combine this with
>> making sure you get the date from when the request actually came in.
>>
>> Patrice
>>
>>
>>
>>
>> --
>> Respect,
>> Shiv Shankar Dayal
>>
>>
>>
>>
>>
>


-- 
Respect,
Shiv Shankar Dayal

Re: [flask] Datetime of the Request

From:
Steven Kryskalla
Date:
2013-09-20 @ 13:23
On Fri, Sep 20, 2013 at 1:34 AM, Alberto Megia <amegia@savemeeting.com> wrote:
> So I was developing a RESTful web service with Flask (Nginx, Gunicorn,
> Flask) and came across with this issue: I want every Gunicorn worker log to
> the same file. I found that Python does not allow multiprocess log to the
> same file, so we decided to use a Queue system (Amazon SQS) to enqueue log
> records and only one consumer process will manage that.

The "have multiple processes log to one file" problem can be solved
using the syslog. Looks like gunicorn has pretty good support for
that:

http://docs.gunicorn.org/en/latest/configure.html#syslog-addr

Using SQS for logging seems a bit heavyweight and adds a point of
failure to your application.

-Steve

Re: [flask] Datetime of the Request

From:
Gary Chambers
Date:
2013-09-19 @ 15:01
Alberto,

> So I was trying to get request's date and inspecting flask.request I have
> seen "date" attribute, but seems like is not working, at least for me
> I get:  None

> from flask import Flask, request
> 
> app = Flask(__name__)
> 
> @app.route('/')
> def hello():
>     print request.date
>
>     return "Hey Flask people"
> 
> if __name__ == '__main__':
>     app.run()

I'm new to Flask, so I would have expected request.date to return something
meaningful as well.  If not by request.date, how about with a before_request
decorator:

from flask import Flask, g
import time

@app.before_request
def before_request():
     g.request_time = time.asctime(time.localtime(time.time()))

Update your code in hello() to print g.request_time.

--
G.