redirect

How to perform HTTP 303 redirect with urllib2 in Python?

How to perform HTTP 303 redirect with urllib2 in Python? Question: I have a URL to fetch, that gives a HTTP 303 redirect : import urllib2 as web import sys url=’http://sample.com’ try: handle=web.urlopen(url) except web.HTTPError, e: print e.code sys.exit(1) data=handle.read() print ‘Result :’ print data So, the above code prints 303 as a result, it’s …

Total answers: 2

Temporarily Redirect stdout/stderr

Temporarily Redirect stdout/stderr Question: Is it possible to temporarily redirect stdout/stderr in Python (i.e. for the duration of a method)? Edit: The problem with the current solutions (which I at first remembered but then forgot) is that they don’t redirect; rather, they just replace the streams in their entirety. Hence, if a method has a …

Total answers: 9

How to get the URL of a redirect with Python

How to get the URL of a redirect with Python Question: In Python, I’m using urllib2 to open a url. This url redirects to another url, which redirects to yet another url. I wish to print out the url after each redirect. For example -> = redirects to A -> B -> C -> D …

Total answers: 3

How To catch python stdout in c++ code

How To catch python stdout in c++ code Question: I have a program which during it’s run sometimes needs to call python in order to preform some tasks. I need a function that calls python and catches pythons stdout and puts it in some file. This is a declaration of the function pythonCallBackFunc(const char* pythonInput) …

Total answers: 3

How do I check if stdin has some data?

How do I check if stdin has some data? Question: In Python, how do you check if sys.stdin has data or not? I found that os.isatty(0) can not only check if stdin is connected to a TTY device, but also if there is data available. But if someone uses code such as sys.stdin = cStringIO.StringIO(“ddd”) …

Total answers: 7

How to redirect with messages to display them in Django Templates?

How to redirect with messages to display them in Django Templates? Question: I have a view that validates and saves a form. After the form is saved, I’d like redirect back to a list_object view with a success message "form for customer xyz was successfully updated…" HttpResponseRedirect doesn’t seem like it would work, because it …

Total answers: 6

Django return redirect() with parameters

Django return redirect() with parameters Question: In my view function I want to call another view and pass data to it : return redirect(‘some-view-name’, backend, form.cleaned_data) , where backend is of registration.backends object, and form.cleaned_data is a dict of form data (but both must be either sent as *args or **kwargs to prevent raising Don’t …

Total answers: 5

Any way to assign terminal output to variable with python?

Any way to assign terminal output to variable with python? Question: I need to grab the duration of a video file via python as part of a larger script. I know I can use ffmpeg to grab the duration, but I need to be able to save that output as a variable back in python. …

Total answers: 6

How to redirect stderr in Python?

How to redirect stderr in Python? Question: I would like to log all the output of a Python script. I tried: import sys log = [] class writer(object): def write(self, data): log.append(data) sys.stdout = writer() sys.stderr = writer() Now, if I “print ‘something’ ” it gets logged. But if I make for instance some syntax …

Total answers: 10

How do I pass template context information when using HttpResponseRedirect in Django?

How do I pass template context information when using HttpResponseRedirect in Django? Question: I have a form that redirects to the same page after a user enters information (so that they can continue entering information). If the form submission is successful, I’m returning HttpResponseRedirect(request.path) which works fine. However, I’d also like to display some messages …

Total answers: 7