9

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?

Sergio
  • 4,537
  • 4
  • 33
  • 41
  • if we have an html file , is there any possibility to create a website using python and display the html data? – Ravi Nov 11 '20 at 17:10

3 Answers3

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':'mymail@gmail.com','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')
NIDHINgL
  • 152
  • 3
  • 6
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.

Acorn
  • 49,061
  • 27
  • 133
  • 172
  • That looks promising but unfortunately we can't change server side code. At least not without a very strong reason. – Sergio Dec 26 '11 at 17:16
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.

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670
  • 1
    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) – Paulo Cheque Aug 24 '13 at 05:27
  • You may also want to change tempfile.NamedTemporaryFile() for tempfile.NamedTemporaryFile(delete=False) – Paulo Cheque Aug 24 '13 at 05:28
  • @PauloCheque: [`webbrowser` adds `file:` to local urls by itself](http://hg.python.org/cpython/file/3.3/Lib/webbrowser.py#l585) – jfs Aug 24 '13 at 06:16
  • @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). – jfs Aug 24 '13 at 06:19