Accessing httplib from a Zope Python script
Hi, I'm a Zope newbie who's more familiar with C# than Python. I've encountered an obstacle and am unable to find what's wrong with my approach. In order to solve the problem, I have searched this group's archive as well as other resources on the web. Some posts earlier this month were related to the problem but didn't address it exactly. I'm trying to implement support for PayPal Instant Payment Notification (IPN). IPN sends a POST to a URL on our Zope server. I decided to implement a Python script to handle the POST. The first task of the script is to send a POST back to PayPal in order to verify the transaction originated from PayPal. To do that, I learned that I could use the features in the httplib module. However, if I have a crappy test script like the following: import httplib result = httplib.HTTP("http://www.paypal.com/cgi-bin/webscr") return result Then I receive the following error when posting to the page: Error Type: Unauthorized Error Value: You are not allowed to access HTTP in this context In my search for help, I learned that Zope allows Script objects to access a subset of the Python modules, so I assumed that was the reason for the error. In my search for help, I happened upon some articles about using External methods to access modules. So I put the "result = httplib..." line into function verifyPayPal, module ipnSupport.py within the Zope\Extensions subfolder. I then added the extension to the same folder containing the Python script. The extension has ID ipnSupport, referening the specified script and function. When I run the following script... result = verifyPayPal return result The following error is returned: Error Type: NameError Error Value: global name 'verifyPayPal' is not defined The same error occurs if I call ipnSupport instead of verifyPayPal. If I try to import module ipnSupport then the following error is returned: Error Type: ImportError Error Value: import of "ipnSupport" is unauthorized So I guess my questions are: 1. Why can I not import httplib into a Zope Python script object? 2. Is an external method only callable from a DTML object? If it can be called from a script object, how do I do that? Thanks for any help or clues you can provide. -- Sean Winstead
On Thu, 2003-03-27 at 15:50, Sean Winstead wrote:
Then I receive the following error when posting to the page:
Error Type: Unauthorized Error Value: You are not allowed to access HTTP in this context
Ignore the issues with your script. Your *real* problem is that PayPal won't accept HTTP for this connection, you must use HTTPS. Posting HTTPS from Zope is a bit more difficult, given that vanilla Python 2.1 doesn't support SSL. What you want is a library called M2Crypto. If you're on Windoze, you're in luck, b/c there are binaries available... but if you're on Linux, you've got some compiling to do. (You'll need SWIG set up before you can compile M2Crypto.) See: http://www.zope.org/Members/dylanr/secure_post for hints on how to proceed once you've got M2Crypto in place. HTH, Dylan
On Thursday 27 March 2003 10:21 am, Dylan Reinhardt wrote:
On Thu, 2003-03-27 at 15:50, Sean Winstead wrote:
Then I receive the following error when posting to the page:
Error Type: Unauthorized Error Value: You are not allowed to access HTTP in this context
Ignore the issues with your script. Your *real* problem is that PayPal won't accept HTTP for this connection, you must use HTTPS.
Posting HTTPS from Zope is a bit more difficult, given that vanilla Python 2.1 doesn't support SSL. What you want is a library called M2Crypto. If you're on Windoze, you're in luck, b/c there are binaries available... but if you're on Linux, you've got some compiling to do. (You'll need SWIG set up before you can compile M2Crypto.)
See: http://www.zope.org/Members/dylanr/secure_post for hints on how to proceed once you've got M2Crypto in place.
Or use libcurl for this - there are python bindings available. -- Gitte Wange Technical Manager Email: gitte@mmmanager.org Web: http://www.mmmanager.org Tlf: +45 36 46 20 02 This breaks the cron job that calls self.Zoo.Diet.LargeAnimals.hippo.feed(), and all the hippos starve. Someone uses self.Zoo.Diet.buildings.visitor_reception.feed(), and ends up filling the reception with hippo food. -- Toby Dickenson, 20 Oct 2000 (Talking about URL Traversal),
Another couple problems you may run into... I have come across a similar situation where I had to make an https connection from Zope. What I did was to create a folder in bin/lib/ which contained my modules. I then used an external method to call the modules. This worked but I started running into some other problems. 1. The socket connection took a long time to create, like about 1 second. Which isn't good for the volume of connections we are making. 2. It is somehow tied into the Zope threads and if the connection is taking a while and someone else executes another connection it would completely hang Zope. I think it has to do with the user thread and it reaching the thread limit of Zope. I have somewhat remedied this by increasing the thread count on startup. If anyone else knows what it could be feel free to chime in. So, what I am attempting to do now is make all of it into a product. I am also going to use the PThreads product to help with making it thread-safe. If you are using windows you can install the dll's that enable you to do https connections from Python, if you are on a *nix box, you can enable ssl in python on the install. I am also interested in what you come up with. Is it going to be an open-source module? :) Scott Burton -----Original Message----- From: zope-admin@zope.org [mailto:zope-admin@zope.org] On Behalf Of Dylan Reinhardt Sent: Thursday, March 27, 2003 1:22 AM To: Sean Winstead Cc: zope@zope.org Subject: Re: [Zope] Accessing httplib from a Zope Python script On Thu, 2003-03-27 at 15:50, Sean Winstead wrote:
Then I receive the following error when posting to the page:
Error Type: Unauthorized Error Value: You are not allowed to access HTTP in this context
Ignore the issues with your script. Your *real* problem is that PayPal won't accept HTTP for this connection, you must use HTTPS. Posting HTTPS from Zope is a bit more difficult, given that vanilla Python 2.1 doesn't support SSL. What you want is a library called M2Crypto. If you're on Windoze, you're in luck, b/c there are binaries available... but if you're on Linux, you've got some compiling to do. (You'll need SWIG set up before you can compile M2Crypto.) See: http://www.zope.org/Members/dylanr/secure_post for hints on how to proceed once you've got M2Crypto in place. HTH, Dylan _______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
see lib/python/Products/PythonScripts/README.txt --On Donnerstag, 27. März 2003 8:50 Uhr -0700 Sean Winstead <seanw@surehand.com> wrote:
Hi,
I'm a Zope newbie who's more familiar with C# than Python. I've encountered an obstacle and am unable to find what's wrong with my approach. In order to solve the problem, I have searched this group's archive as well as other resources on the web. Some posts earlier this month were related to the problem but didn't address it exactly.
I'm trying to implement support for PayPal Instant Payment Notification (IPN). IPN sends a POST to a URL on our Zope server. I decided to implement a Python script to handle the POST. The first task of the script is to send a POST back to PayPal in order to verify the transaction originated from PayPal.
To do that, I learned that I could use the features in the httplib module. However, if I have a crappy test script like the following:
import httplib result = httplib.HTTP("http://www.paypal.com/cgi-bin/webscr") return result
Then I receive the following error when posting to the page:
Error Type: Unauthorized Error Value: You are not allowed to access HTTP in this context
In my search for help, I learned that Zope allows Script objects to access a subset of the Python modules, so I assumed that was the reason for the error. In my search for help, I happened upon some articles about using External methods to access modules. So I put the "result = httplib..." line into function verifyPayPal, module ipnSupport.py within the Zope\Extensions subfolder. I then added the extension to the same folder containing the Python script. The extension has ID ipnSupport, referening the specified script and function. When I run the following script...
result = verifyPayPal return result
The following error is returned:
Error Type: NameError Error Value: global name 'verifyPayPal' is not defined
The same error occurs if I call ipnSupport instead of verifyPayPal. If I try to import module ipnSupport then the following error is returned:
Error Type: ImportError Error Value: import of "ipnSupport" is unauthorized
So I guess my questions are:
1. Why can I not import httplib into a Zope Python script object?
2. Is an external method only callable from a DTML object? If it can be called from a script object, how do I do that?
Thanks for any help or clues you can provide.
-- Sean Winstead
_______________________________________________ Zope maillist - Zope@zope.org http://mail.zope.org/mailman/listinfo/zope ** No cross posts or HTML encoding! ** (Related lists - http://mail.zope.org/mailman/listinfo/zope-announce http://mail.zope.org/mailman/listinfo/zope-dev )
From Andreas Jung: see lib/python/Products/PythonScripts/README.txt
Useful info. Thanks for the recommendation. Sounds like I shouldn't make httplib accessible though.
From Geir Bækholt: result = context.verifyPayPal()
Thanks for the tip! This makes sense. I read about context and how I can get to other objects through it, but wasn't yet smart enough to realize it would help in this situation <g>.
From Dylan Reinhardt: Ignore the issues with your script. Your *real* problem is that PayPal won't accept HTTP for this connection, you must use HTTPS.
Yes, I saw M2Crypto referenced in your article as well as some other places. I figured I would tackle this obstacle first and then worry about HTTPS after that. While looking at M2Crypto, I decided to install OpenSSL and M2Crypto on my PC (fortunately I am running Windows). So I'm ready to try it out. -- Sean Winstead
On Thu, 27 Mar 2003 08:50:53 -0700 GMT (..16:50 where i live(GMT+1) ) Sean Winstead asked the Zope mailinglist about the following: ..... SW> So I put the "result = SW> httplib..." line into function verifyPayPal, module ipnSupport.py within SW> the Zope\Extensions subfolder. I then added the extension to the same SW> folder containing the Python script. The extension has ID ipnSupport, SW> referening the specified script and function. When I run the following SW> script... SW> result = verifyPayPal SW> return result SW> The following error is returned: SW> Error Type: NameError SW> Error Value: global name 'verifyPayPal' is not defined result = context.verifyPayPal() should do the trick :) -- Geir Bækholt
participants (6)
-
Andreas Jung -
Dylan Reinhardt -
Geir Bækholt -
Gitte Wange -
Scott Burton -
Sean Winstead