Re: Calling a WSGI application from an action

2008-06-09 Thread Alberto Valverde



 Alberto Valverde wrote:
 I had a similar problem when mounting Trac inside a Pylons application
 as a controller. Apparently, at some stage environ['wsgi.input'] was
 consumed hence POST requests were seen blank by Trac once they reached
 it.

 I solved it using this piece of middleware [1] stacked closest to the
 server [2] to cache input in a regular temporary file so it can be
 'rewound' before passing the request to Trac [3].

 WebOb had some functions to do this, but after thinking about it I made
 it a bit easier.  With WebOb trunk you can now do:

req = Request(environ)
req.make_body_seekable()

 And then at any time you can do req.body_file.seek(0) (or
 environ['wsgi.input'].seek(0)) before sending the request on to another
 application.  req.copy() also does this, but if the body has been eaten
 by something like paste.request.parse_formvars (what all but the tip of
 Pylons uses, I think) then it won't really work, so you have to prep the
 environment this way.

This looks very useful, I'll probably rewrite the code I posted sometime
to make use of this feature and make it simpler.

 It's about the same thing as what you did, but
 you'd be better off using tempfile,

tempfile has actually been one of my most recent discoveries in the
standard library :) Just wanted to get rid of the annoying os.tempnam
warning so I reinvented the wheel.

Alberto


--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---



Re: Calling a WSGI application from an action

2008-06-03 Thread Alberto Valverde

Alex Marandon wrote:
 2008/5/30 Ian Bicking [EMAIL PROTECTED]:
   
 ()

 However, there is a problem that the POST body can be eaten up.  WebOb
 specifically tries to reconstruct it, but not for file uploads (simply
 because I didn't get around to it, because it's harder to construct than
 a simple POST form).  And maybe what you are encountering is this same
 issue.
 

 Hi Ian, thanks a lot for answering my message.

 One thing I don't understand, is why the POST body gets
 deconstructed in the first place. I don't have a deep understanding
 of WSGI yet, but I thought one of its advantages was to allow
 developers to build arbitrary chains of middlewares and applications
 that are not aware of each others. So how come Pylons is modifying
 the POST body in such a way that it can't be used by subsequent WSGI
 component in the chain?
   

I had a similar problem when mounting Trac inside a Pylons application
as a controller. Apparently, at some stage environ['wsgi.input'] was
consumed hence POST requests were seen blank by Trac once they reached it.

I solved it using this piece of middleware [1] stacked closest to the
server [2] to cache input in a regular temporary file so it can be
'rewound' before passing the request to Trac [3].

Hope it helps,
Alberto

[1]
http://beta.toscawidgets.org/trac/twWebSite/browser/twwebsite/lib/inputsaver.py
[2]
http://beta.toscawidgets.org/trac/twWebSite/browser/twwebsite/config/middleware.py#L99
[3]
http://beta.toscawidgets.org/trac/twWebSite/browser/twwebsite/controllers/tracdelegate.py#L16

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---



Re: Calling a WSGI application from an action

2008-06-03 Thread Alex Marandon

2008/6/2 Ian Bicking [EMAIL PROTECTED]:
 You need code that takes req.POST and turns it into the serialized body,
 so that the WSGI app you are calling can reconstruct that body.

 I understand that Pylons parses the body of the POST request and turns
 into a Python data structure. In particular, any uploaded file gets
 turned into a cgi.FieldStorage. What I need to do is convert it back
 to its serialized version. Am I correct so far?

 Then where should I set the serialized version so that it can be used
 by oher WSGI apps? It seems that request.POST can't be assigned a new
 value.

 You'd put a new file-like object into wsgi.input, like:

 from cStringIO import StringIO
 environ['wsgi.input'] = StringIO(serialized_body)

Great, I managed to make it working. I'll try to get my head around
webob and add it there.
Thanks a lot.

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---



Re: Calling a WSGI application from an action

2008-06-02 Thread Alex Marandon

2008/5/30 Ian Bicking [EMAIL PROTECTED]:
 I'm trying to apply the recipe from
 http://wiki.pylonshq.com/display/pylonsdocs/Web+Server+Gateway+Interface+Support#running-a-wsgi-application-from-within-a-controller

 Unfortunately it works only for the file management features, not for
 the upload. After digging into FCKeditor's Python code I figured out
 that the WSGI app doesn't get all the data it needs from from the WSGI
 environment. I dumped the WSGI environment when running the app under
 mod_wsgi directly and within Pylons and there happen to be a lot of
 differences between the two. I'm wondering if there would be a way to
 get access to the original WSGI environment, before it gets modified
 by Pylons, so that it would be compatible with what a regular WSGI
 application expect.

 WebOb makes this easier, with:

 resp = req.get_response(fckeditor_wsgi)

 If you aren't using a very new version of Pylons, you can make a webob
 request with webob.Request(request.environ)

 However, there is a problem that the POST body can be eaten up.  WebOb
 specifically tries to reconstruct it, but not for file uploads (simply
 because I didn't get around to it, because it's harder to construct than
 a simple POST form).  And maybe what you are encountering is this same
 issue.

Hi Ian, thanks a lot for answering my message.

One thing I don't understand, is why the POST body gets
deconstructed in the first place. I don't have a deep understanding
of WSGI yet, but I thought one of its advantages was to allow
developers to build arbitrary chains of middlewares and applications
that are not aware of each others. So how come Pylons is modifying
the POST body in such a way that it can't be used by subsequent WSGI
component in the chain?

 This recipe roughly describes what a file upload looks like:
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306

 You need code that takes req.POST and turns it into the serialized body,
 so that the WSGI app you are calling can reconstruct that body.

I understand that Pylons parses the body of the POST request and turns
into a Python data structure. In particular, any uploaded file gets
turned into a cgi.FieldStorage. What I need to do is convert it back
to its serialized version. Am I correct so far?

Then where should I set the serialized version so that it can be used
by oher WSGI apps? It seems that request.POST can't be assigned a new
value.

 I'd be
 happy for webob to be able to do this, if you are inclined to add it
 there -- the code is in webob.FakeCGIBody._get_body(), and the instance
 would have to be aware of the Content-Type of the upload, and if it's
 multipart/form-data then it would have to reconstruct the body
 differently, in line with what that recipe does.

I'd be happy to add it once I get a sufficient understanding of what
I'm doing. I should probably do it outside of WebOb to start with and
then I'll try to integrate it into WebOb so that it can be easily
reused.

Regards,
Alex

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---



Re: Calling a WSGI application from an action

2008-06-02 Thread Ian Bicking

Alex Marandon wrote:
 2008/5/30 Ian Bicking [EMAIL PROTECTED]:
 I'm trying to apply the recipe from
 http://wiki.pylonshq.com/display/pylonsdocs/Web+Server+Gateway+Interface+Support#running-a-wsgi-application-from-within-a-controller

 Unfortunately it works only for the file management features, not for
 the upload. After digging into FCKeditor's Python code I figured out
 that the WSGI app doesn't get all the data it needs from from the WSGI
 environment. I dumped the WSGI environment when running the app under
 mod_wsgi directly and within Pylons and there happen to be a lot of
 differences between the two. I'm wondering if there would be a way to
 get access to the original WSGI environment, before it gets modified
 by Pylons, so that it would be compatible with what a regular WSGI
 application expect.
 WebOb makes this easier, with:

 resp = req.get_response(fckeditor_wsgi)

 If you aren't using a very new version of Pylons, you can make a webob
 request with webob.Request(request.environ)

 However, there is a problem that the POST body can be eaten up.  WebOb
 specifically tries to reconstruct it, but not for file uploads (simply
 because I didn't get around to it, because it's harder to construct than
 a simple POST form).  And maybe what you are encountering is this same
 issue.
 
 Hi Ian, thanks a lot for answering my message.
 
 One thing I don't understand, is why the POST body gets
 deconstructed in the first place. I don't have a deep understanding
 of WSGI yet, but I thought one of its advantages was to allow
 developers to build arbitrary chains of middlewares and applications
 that are not aware of each others. So how come Pylons is modifying
 the POST body in such a way that it can't be used by subsequent WSGI
 component in the chain?

Generally yes, but this specifically is a problem.  If you don't access 
request.POST (or request.params) it won't read the body, but it's 
possible something in Pylons accesses one of these early on.

 This recipe roughly describes what a file upload looks like:
 http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306

 You need code that takes req.POST and turns it into the serialized body,
 so that the WSGI app you are calling can reconstruct that body.
 
 I understand that Pylons parses the body of the POST request and turns
 into a Python data structure. In particular, any uploaded file gets
 turned into a cgi.FieldStorage. What I need to do is convert it back
 to its serialized version. Am I correct so far?
 
 Then where should I set the serialized version so that it can be used
 by oher WSGI apps? It seems that request.POST can't be assigned a new
 value.

You'd put a new file-like object into wsgi.input, like:

from cStringIO import StringIO
environ['wsgi.input'] = StringIO(serialized_body)

-- 
Ian Bicking : [EMAIL PROTECTED] : http://blog.ianbicking.org

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---



Calling a WSGI application from an action

2008-05-30 Thread Alex Marandon

Hello,

I'm trying to integrate FCKeditor's file management and upload
features into a Pylons app. FCKeditor ships with a WSGI application to
handle the server side of these features. I tried to run that
application under mod_wsgi and it works fine. Now I'd like to
integrate that within my Pylons app to take advantage of my
authentication mechanism, so that only registered users can upload
files.

I'm trying to apply the recipe from
http://wiki.pylonshq.com/display/pylonsdocs/Web+Server+Gateway+Interface+Support#running-a-wsgi-application-from-within-a-controller

Unfortunately it works only for the file management features, not for
the upload. After digging into FCKeditor's Python code I figured out
that the WSGI app doesn't get all the data it needs from from the WSGI
environment. I dumped the WSGI environment when running the app under
mod_wsgi directly and within Pylons and there happen to be a lot of
differences between the two. I'm wondering if there would be a way to
get access to the original WSGI environment, before it gets modified
by Pylons, so that it would be compatible with what a regular WSGI
application expect.

Another minor issue I've got is that the HTTP Content-Type header
(text/xml in this case) is not sent properly when calling the WSGI app
from Pylons. That one is not very critical as I can set it with the
controller action which calls the WSGI app.

Any help would be much appreciated, as I'm beginning to pull my hair on this.
Thanks,
Alex

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---



Re: Calling a WSGI application from an action

2008-05-30 Thread Ian Bicking

Alex Marandon wrote:
 Hello,
 
 I'm trying to integrate FCKeditor's file management and upload
 features into a Pylons app. FCKeditor ships with a WSGI application to
 handle the server side of these features. 

That's cool, that's one of the first cases I've heard of a Javascript 
product shipping something in WSGI (usually it's just PHP).

 I tried to run that
 application under mod_wsgi and it works fine. Now I'd like to
 integrate that within my Pylons app to take advantage of my
 authentication mechanism, so that only registered users can upload
 files.
 
 I'm trying to apply the recipe from
 http://wiki.pylonshq.com/display/pylonsdocs/Web+Server+Gateway+Interface+Support#running-a-wsgi-application-from-within-a-controller
 
 Unfortunately it works only for the file management features, not for
 the upload. After digging into FCKeditor's Python code I figured out
 that the WSGI app doesn't get all the data it needs from from the WSGI
 environment. I dumped the WSGI environment when running the app under
 mod_wsgi directly and within Pylons and there happen to be a lot of
 differences between the two. I'm wondering if there would be a way to
 get access to the original WSGI environment, before it gets modified
 by Pylons, so that it would be compatible with what a regular WSGI
 application expect.

WebOb makes this easier, with:

resp = req.get_response(fckeditor_wsgi)

If you aren't using a very new version of Pylons, you can make a webob 
request with webob.Request(request.environ)

However, there is a problem that the POST body can be eaten up.  WebOb 
specifically tries to reconstruct it, but not for file uploads (simply 
because I didn't get around to it, because it's harder to construct than 
a simple POST form).  And maybe what you are encountering is this same 
issue.

This recipe roughly describes what a file upload looks like: 
http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/146306

You need code that takes req.POST and turns it into the serialized body, 
so that the WSGI app you are calling can reconstruct that body.  I'd be 
happy for webob to be able to do this, if you are inclined to add it 
there -- the code is in webob.FakeCGIBody._get_body(), and the instance 
would have to be aware of the Content-Type of the upload, and if it's 
multipart/form-data then it would have to reconstruct the body 
differently, in line with what that recipe does.

 Another minor issue I've got is that the HTTP Content-Type header
 (text/xml in this case) is not sent properly when calling the WSGI app
 from Pylons. That one is not very critical as I can set it with the
 controller action which calls the WSGI app.

I can't think of anything that would change that header; maybe it wasn't 
set properly on the request to start with?


-- 
Ian Bicking : [EMAIL PROTECTED] : http://blog.ianbicking.org

--~--~-~--~~~---~--~~
You received this message because you are subscribed to the Google Groups 
pylons-discuss group.
To post to this group, send email to pylons-discuss@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/pylons-discuss?hl=en
-~--~~~~--~~--~--~---