Python: Request URL via POST and show result in browser
Asked Answered
M

4

10

I'm a developer for a big GUI app and we have a web site for bug tracking. Anybody can submit a new bug to the bug tracking site. We can detect certain failures from our desktop app (i.e. an unhandled exception) and in such cases we would like to open the submit-new-bug form in the user predefined browser, adding whatever information we can gather about the failure to some form fields. We can either retrieve the submit-new-bug form using GET or POST http methods and we can provide default field values to that form. So from the http server side everything is pretty much OK.

So far we can successfully open a URL passing the default values as GET parameters in the URL using the webbrowser module from the Python Standard Library. There are, however, some limitations of this method such as the maximum allowed length of the URL for some browsers (specially MS IE). The webbrowser module doesn't seem to have a way to request the URL using POST. OTOH there's the urllib2 module that provides the type of control we want but AFAIK it lacks the possibility of opening the retrieved page in the user preferred browser.

Is there a way to get this mixed behavior we want (to have the fine control of urllib2 with the higher level functionallity of webbrowser)?

PS: We have thought about the possibility of retreiving the URL with urllib2, saving its content to a temp file and opening that file with webbrowser. This is a little nasty solution and in this case we would have to deal with other issues such as relative URLs. Is there a better solution?

Marengo answered 26/12, 2011 at 16:9 Comment(1)
if we have an html file , is there any possibility to create a website using python and display the html data?Update
E
9

This is not proper answer. but it also work

import requests
import webbrowser

url = "https://www.facebook.com/login/device-based/regular/login/?login_attempt=1&lwv=110"
myInput = {'email':'[email protected]','pass':'mypaass'}
x = requests.post(url, data = myInput)
y = x.text
f = open("home.html", "a")
f.write(y)
f.close()
webbrowser.open('file:///root/python/home.html')
Erlene answered 20/7, 2019 at 19:38 Comment(0)
X
6

I don't know of any way you can open the result of a POST request in a web browser without saving the result to a file and opening that.

What about taking an alternative approach and temporarily storing the data on the server. Then the page can be opened in the browser with a simple id parameter, and the saved partially filled form would be shown.

Xenon answered 26/12, 2011 at 16:36 Comment(1)
That looks promising but unfortunately we can't change server side code. At least not without a very strong reason.Marengo
F
2

You could use tempfile.NamedTemporaryFile():

import tempfile
import webbrowser
import jinja2

t = jinja2.Template('hello {{ name }}!') # you could load template from a file
f = tempfile.NamedTemporaryFile() # deleted when goes out of scope (closed)
f.write(t.render(name='abc'))
f.flush()
webbrowser.open_new_tab(f.name) # returns immediately 

A better approach if the server can be easily modified is to make POST request with partial parameters using urllib2 and open url generated by server using webbrowser as suggested by @Acorn.

Falmouth answered 26/12, 2011 at 17:4 Comment(4)
The command "webbrowser.open_new_tab(f.name)" will not work in Mac OS. You will have to include the protocol too: webbrowser.open_new_tab('file://' + f.name)Brakeman
You may also want to change tempfile.NamedTemporaryFile() for tempfile.NamedTemporaryFile(delete=False)Brakeman
@PauloCheque: webbrowser adds file: to local urls by itselfFalmouth
@PauloCheque: delete=True enables automatic cleanup. Notice: that I use f.flush() (make data available to the OS) instead of f.close() (close and delete the file).Falmouth
N
0

If using selenium, instead of writing to a file, it is possible to inject a script to write the HTML page generated from the response:

response = requests.post('https://xxx', data = ...)
response_html = response.text 
driver.get("about:blank")
driver.execute_script("document.write(arguments[0])", response_html)
Nahshun answered 12/11, 2023 at 16:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.