using "request" variable in python web program
Hi I am trying to write a facebook application in python - I have been programming simple desktop applications till now and am not really familiar with web apps Pyfacebook is the wrapper for the REST based Facebook API - there is a simple example for its usage as shown below: def simple_web_app(request, api_key, secret_key): fb = Facebook(api_key, secret_key, request.GET['auth_token']) fb.auth_getSession() A Django-based tutorial and source is available here: http://wiki.developers.facebook.com/index.php/PythonPyFacebookTutorial I really don't want to use a framework for the time being and just want an app up and running My question is - how do you create the "request" object which will be passed into the simple_web_app(request, api_key, secret_key) function here - is there somewhere I can read into it? I have been googling for hours and there seems to be no simple, straightforward example for this - PHP rules when it comes to simple web programming apps - either I am not looking at the right places but it's very hard to get up and running quickly for web apps in Python - maybe DiveIntoPython needs a section devoted for web programming -- http://mail.python.org/mailman/listinfo/python-list
Re: using "request" variable in python web program
> However, it shouldn't be too bad to set Django up, really, especially > if people have taken some time to explain something very close to what > you want to do using Django. Moreover, there's a Google group (django- > users) where you could ask general questions, and I imagine that > people will be quite happy to help you out. But I can totally > understand that creating databases and installing various packages > seems somewhat peripheral to a task which could arguably be done using > a CGI script (as far as I can see). > Thanks a ton Paul for the information. I am using CGI and my host (nearlyfreespeech.net) does not have django hosting - and being mainly a C/dekstop apps programmer I really don't want to learn the whole MVC concept and its implementation (and quirks) in django - I want to use abstractions only when I feel the need for them Since a django user made pyfacebook, hopefully someone on the forum will help out Thanks again Sami > Paul > > P.S. I'm not too impressed by the lack of a common high-level Web API > for Python, either. However, I've mostly ignored the discussions and > stuck with my own API (WebStack) in the knowledge that if it doesn't > work for me, at least I know how I might fix it. -- http://mail.python.org/mailman/listinfo/python-list
Re: using "request" variable in python web program
> See the library reference for information on how to get the value of > form/request parameters from the CGI environment: > > http://docs.python.org/lib/node561.html > > Paul Thanks again Paul - now I can see that the "request" object is a string of name/value pairs that occurs after a "?" in a url e.g. url.com/index.cgi?name=sami&lang=python - is that correct? Googling for this information is useless since the word "request" is so common on the www -- http://mail.python.org/mailman/listinfo/python-list
Re: using "request" variable in python web program
> Not exactly - this is the "query string" part of the URI. > Request and Response are the two messages defined by the HTTP protocol. > When you type a URL or click on a link or press a button in a page, your > browser builds the appropiate Request message and sends it to the server. > After processing, the server emits the Response message, and the browser > displays it or otherwise processes the response. > > -- > Gabriel Genellina Thanks Paul and Gabriel - I am confused I guess - I do know about the request/response mechanism, I wrote this app a while ago: http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/522983 - I wrote this app before in C and decoded request/response params using a sniffer - I ported it to Python since I wanted to learn Python > The string after "?" in a URL is actually the "query string" and is > typically exposed as the QUERY_STRING environment variable in CGI. See > here for more specific details: This is what is mixing me up - an example given in the source for Pyfacebook - http://pyfacebook.googlecode.com/svn/trunk/examples/examples.py def simple_web_app(request, api_key, secret_key): fb = Facebook(api_key, secret_key, request.GET['auth_token']) fb.auth.getSession() It seemed to me "request" was a key in a key/value pair string/ dictionary Anyway, I have the "auth_token" now and I can pass these 3 string values to as Facebook(, , ) - and it's moving along - but I am persevering :) no PHP for me - I hope I can put up a tut for this afterwards Thanks again for the help, guys -- http://mail.python.org/mailman/listinfo/python-list
Problems trying to hook own exception function to sys.excepthook
Hello, I am new to Python. I tried to hook my own ExceptionPrintingFunction sys.excepthook but it does not work. This is what I wrote: --- import sys def MyOwnExceptHook(typ, val, tb): print "Inside my own hook" sys.excepthook = MyOwnExceptHook x = 1/0 --- This is what I get --- Traceback (most recent call last): File "E:/Home/Programming/Python/TryProjects/ExceptHandling1/Except5.py", line 8, in x = 1/0 ZeroDivisionError: integer division or modulo by zero --- I never see "Inside my own hook" which tells me that the hook is not being called. What I really want to test is to stop the exception from propagating further and leave the program intact. What am I doing wrong? Please let me know if there are any other newbie groups that I should probably try in stead. Thanks Sami -- http://mail.python.org/mailman/listinfo/python-list
Re: Problems trying to hook own exception function to sys.excepthook
Peter Otten wrote: > Sami wrote: > >> Hello, >> >> I am new to Python. >> >> I tried to hook my own ExceptionPrintingFunction sys.excepthook but it >> does not work. >> >> This is what I wrote: >> --- >> import sys >> >> def MyOwnExceptHook(typ, val, tb): >> print "Inside my own hook" >> >> sys.excepthook = MyOwnExceptHook >> >> x = 1/0 >> --- >> This is what I get >> --- >> Traceback (most recent call last): >>File >> "E:/Home/Programming/Python/TryProjects/ExceptHandling1/Except5.py", >> line 8, in >> x = 1/0 >> ZeroDivisionError: integer division or modulo by zero >> --- >> >> I never see "Inside my own hook" which tells me that the hook is not >> being called. What I really want to test is to stop the exception from >> propagating further and leave the program intact. >> >> What am I doing wrong? Please let me know if there are any other newbie >> groups that I should probably try in stead. > > Are you running your code from within idle? It wraps your script in > something like > > try: ># run script > except: ># show traceback > > (Have a look at InteractiveInterpreter.runcode() in code.py if you are > interested in the actual code) > > So your except hook never gets to see the exception and therefore won't be > invoked. Run your script from the commandline and you will see the > behaviour you expected. > > Peter > Great!! Thank you. I have been trying since yesterday to get to the bottom of this. Does this mean that one should always use the command line to run a python program? Sami -- http://mail.python.org/mailman/listinfo/python-list
traceback.print_exc() supposed to stop exception propagation.
Hello, In the Python book that I am using to learn the language it says that the traceback.print_exc() can be used to stop exception propagation and make the program keep running. Here is a simple piece of code that I typed in to test this fact: --- import sys def Myexcepthook(etype, value, tb): print "in Myexcepthook\n" import traceback lines=traceback.format_exception(etype, value, tb) print "\n".join(lines) traceback.print_exc() sys.excepthook = Myexcepthook x = 1/0 x = 78 print x -- The Output: -- in Myexcepthook Traceback (most recent call last): File "E:\Home\Programming\Python\TryProjects\ExceptHandling1\Except2.py", lin 15, in x = 1/0 ZeroDivisionError: integer division or modulo by zero None -- I never see the value 78. What am I doing wrong? Thanks, Sami -- http://mail.python.org/mailman/listinfo/python-list
RE: Python 3.3 shuts down
Hi all, Python 3.3 IDLE opens perfectly from that If I am try to open any-python-file.py or New file the python IDLE closes automatically. Python Version 3.3OS Windows 7 This problem happened when I had installed py2exe to convert into Python executables, since I have tried other packages like cx_freeze, PyInstaller with that I wasn't successful. Please provide me a solution, thanks Best Regards --Ramas -- https://mail.python.org/mailman/listinfo/python-list
RE: Compiling Error prompt
Hi all, when ever I execute the code always on the output prompts this error "C:\Python33\lib\importlib\_bootstrap.py:1532: UserWarning: Module six was already imported from C:\Python33\lib\site-packages\six.py, but c:\python33\lib\site-packages\six-1.6.1-py3.3.egg is being added to sys.path loader.load_module(name)" How to fix this error Thanks in advance... Best Regards,Ramas. -- https://mail.python.org/mailman/listinfo/python-list
Getting error message/trace over the C API
Hello group, I'm writing a C++ based application that embeds the python engine. Now I have a problem regarding exception/error information. Is there a way to get the exception message and possibly the traceback into a string for example? I've been eyeballing the PyErr_ module and it seems fairly limited. In other words PyErr_Print() calls the right functions for getting the exception information but unfortunately it is hardwired to print this data directly into sys.stderr, and for an embedded application this is completely inappropriate. I have seen solutions that propose writing a custom Python class with write method and using that to grab the output from sys.stderr and then saving the data for example into a variable for later access, but unless Im mistaken this solution (not only that it is extremely ugly) is not thread safe. Even if a thread in my application is holding GIL, the lock can be released by extensions/the interpreter and thus opens up a race condition regarding that grabber object. Please advice how to do this. -sami -- http://mail.python.org/mailman/listinfo/python-list
traceback over C API and PyObject_CallObject
Hello group, I'm trying to get the Python exception information (message and traceback) stored into a string in my C++ code. However all i get back is the string "None". All the checks pass and all pointers get a value from the python API calls. I've also tried with a different function such as PyObject_CallFunctionObjArgs but the result is the same. Thanks void get_python_exception(string& message, string& traceback) { GIL g; PyObject* type = NULL; PyObject* value = NULL; PyObject* trace = NULL; PyErr_Fetch(&type, &value, &trace); py_ref ref_type(type); py_ref ref_value(value); py_ref ref_trace(trace); py_ref name(PyString_FromString("traceback")); py_ref module(PyImport_Import(name.get())); if (module) { py_ref fun(PyObject_GetAttrString(module.get(), "format_exc")); if (fun) { PyErr_Restore(type, value, trace); ref_type.release(); ref_value.release(); ref_trace.release(); py_ref ret(PyObject_CallObject(fun.get(), NULL)); if (ret && PyString_Check(ret.get())) { char* str = PyString_AsString(ret.get()); message = str; traceback = "traceback not available"; return; } } } message = "message not available"; traceback = "traceback not available"; } -- http://mail.python.org/mailman/listinfo/python-list
Re: Getting error message/trace over the C API
On Tue, 16 Oct 2007 18:55:22 +, Duncan Booth wrote: > Sami Vaisanen <[EMAIL PROTECTED]> wrote: > >> Hello group, >> >> I'm writing a C++ based application that embeds the python engine. Now I >> have a problem regarding exception/error information. Is there a way to >> get the exception message and possibly the traceback into a string for >> example? I've been eyeballing the PyErr_ module and it seems fairly >> limited. In other words PyErr_Print() calls the right functions for >> getting the exception information but unfortunately it is hardwired to >> print this data directly into sys.stderr, and for an embedded application >> this is completely inappropriate. >> >> >> Please advice how to do this. >> > All you have to do is call whatever functions you would call from Python. > e.g. from C you need to import traceback, then call getattr to get the > appropriate function (e.g. format_exc or format_exception), then just call > it. void get_python_exception(string& message, string& traceback) { GIL g; PyObject* type = NULL; PyObject* value = NULL; PyObject* trace = NULL; PyErr_Fetch(&type, &value, &trace); py_ref ref_type(type); py_ref ref_value(value); py_ref ref_trace(trace); py_ref name(PyString_FromString("traceback")); py_ref module(PyImport_Import(name.get())); if (module) { py_ref fun(PyObject_GetAttrString(module.get(), "format_exc")); if (fun) { PyErr_Restore(type, value, trace); ref_type.release(); ref_value.release(); ref_trace.release(); py_ref ret(PyObject_CallObject(fun.get(), NULL)); if (ret && PyString_Check(ret.get())) { char* str = PyString_AsString(ret.get()); message = str; traceback = "traceback not available"; return; } } } message = "message not available"; traceback = "traceback not available"; } str evaluates to "None", any ideas what gives here? I've also tried to call the traceback with a different function, such as PyObject_CallFunctionObjArgs but the result is still same. Thanks -- http://mail.python.org/mailman/listinfo/python-list
Re: Extending Python with C API
On Thu, 13 Sep 2007 21:26:33 -0400, Carsten Haese wrote: > On Thu, 2007-09-13 at 18:05 -0700, Thierry Masson wrote: >> Hello, >> >> I'm trying to use the C API to extend Python. I've looked at various >> books and web sites (including the docs at python.org) and I can't get >> any of the samples to work. Below is a very minimalist example that >> I'm trying to get working and I was wondering if someone could tell me >> where I'm going wrong. It's probably something very obvious to those >> familiar with this technique. >>[snip...] >> The setup script builds the library OK, and I've verified that >> gtestmodule.so is getting created. But when I try to run a test script >> ( test.py, above) that uses the library, I get this error: >> >> Traceback (most recent call last): >> File "test.py", line 2, in ? >> import gtestmodule >> ImportError: ./gtestmodule.so: undefined symbol: PyBuildValue > > Your module C code uses an unknown function by the name of PyBuildValue. > The actual name of the function you mean is Py_BuildValue. > > HTH, void get_python_exception(string& message, string& traceback) { GIL g; PyObject* type = NULL; PyObject* value = NULL; PyObject* trace = NULL; PyErr_Fetch(&type, &value, &trace); py_ref ref_type(type); py_ref ref_value(value); py_ref ref_trace(trace); py_ref name(PyString_FromString("traceback")); py_ref module(PyImport_Import(name.get())); if (module) { py_ref fun(PyObject_GetAttrString(module.get(), "format_exc")); if (fun) { PyErr_Restore(type, value, trace); ref_type.release(); ref_value.release(); ref_trace.release(); py_ref ret(PyObject_CallObject(fun.get(), NULL)); if (ret && PyString_Check(ret.get())) { char* str = PyString_AsString(ret.get()); message = str; traceback = "traceback not available"; return; } } } message = "message not available"; traceback = "traceback not available"; } str evaluates to "None", any ideas what gives here? I've also tried to call the traceback with a different function, such as PyObject_CallFunctionObjArgs but the result is still same. Thanks -- http://mail.python.org/mailman/listinfo/python-list
traceback over C API - still not working...
This is becoming utterly painful process I found out that the return value from "format_exception" function is NOT a list, i.e. PyList_Check() fails. PySequence_Check() succeeds but then PySequence_List() gives me back -1. So wtf? I must say the API is crap on this part. Im trying to get error information regarding previous error and if all i get back is another error indicator, then what am I supposed to do? Recursive error handling? Sounds like da bomb! The API should have two levels of error handling. First level should expose a conventional error code mechanism for checking for bad params etc. Only if the error code says that there was a python error during evaluation/compilation should one need to use this mechanism. Just my two cents. Thanks for help. void get_python_error_info(string& info) { GIL g; PyObject* type = NULL; PyObject* value = NULL; PyObject* trace = NULL; PyErr_Fetch(&type, &value, &trace); py_ref ref_type(type); py_ref ref_value(value); py_ref ref_trace(trace); py_ref name(PyString_FromString("traceback")); py_ref module(PyImport_Import(name.get())); if (!module) { PyErr_Clear(); return; } PyObject* list = NULL; if (trace) { py_ref fun(PyObject_GetAttrString(module.get(), "format_exception")); if (fun) list = PyObject_CallFunctionObjArgs(type, value, trace, NULL); PyErr_Clear(); } else { py_ref fun(PyObject_GetAttrString(module.get(), "format_exception_only")); if (fun) list = PyObject_CallFunctionObjArgs(type, value, NULL); PyErr_Clear(); } if (list && PySequence_Check(list)) { Py_ssize_t len = PySequence_Size(list); for (Py_ssize_t i=0; i Hello group, > > I'm trying to get the Python exception information (message and > traceback) > stored into a string in my C++ code. However all i get back is the string > "None". This is what you get (actually "None\n") when there is no error set. > All the checks pass and all pointers get a value from the python > API calls. I've also tried with a different function such as > PyObject_CallFunctionObjArgs but the result is the same. Since you already know the three components (type, value, trace), I'd use traceback.format_exception instead (and remove the PyErr_Restore call - I'm unsure if it works the way you expect it). In this case you have to pass three arguments, so yes, use PyObject_CallFunctionObjArgs (remember the final NULL). Beware: format_exception returns a formatted list, not a string. You have to concatenate all the elements (either using ''.join or repeteadly calling PyString_Concat) > void get_python_exception(string& message, string& traceback) > { > GIL g; >PyObject* type = NULL; > PyObject* value = NULL; > PyObject* trace = NULL; >PyErr_Fetch(&type, &value, &trace); >py_ref ref_type(type); > py_ref ref_value(value); > py_ref ref_trace(trace); >py_ref name(PyString_FromString("traceback")); > py_ref module(PyImport_Import(name.get())); > if (module) > { > py_ref fun(PyObject_GetAttrString(module.get(), "format_exc")); > if (fun) > { > PyErr_Restore(type, value, trace); > ref_type.release(); > ref_value.release(); > ref_trace.release(); >py_ref ret(PyObject_CallObject(fun.get(), NULL)); > if (ret && PyString_Check(ret.get())) > { > char* str = PyString_AsString(ret.get()); > message = str; > traceback = "traceback not available"; > return; > } > } > } > message = "message not available"; > traceback = "traceback not available"; > } -- Gabriel Genellina -- http://mail.python.org/mailman/listinfo/python-list