Python: request url via POST and show result in browser

I am a developer of a large graphical application and we have a website for tracking bugs. Anyone can post a new bug to the bug tracking site. We can detect certain crashes in our desktop application (that is, an unhandled exception), and in such cases we would like to open the submit-new-bug form in a user-predefined browser, adding any information that we can collect about the crash to some form fields , We can either get the submit-new-bug form using the GET or POST http methods, and we can provide default field values ​​for this form. So on the http server side, everything is fine.

So far, we can successfully open the URL by passing the default values ​​as GET parameters to the URL using the webbrowser module from the Python standard library. However, there are some limitations to this method, such as the maximum allowable URL length for some browsers ( especially MS IE ). The webbrowser module does not seem to have a way to request a URL using POST. OTOH is the urllib2 module which provides the urllib2 type of control, but AFAIK does not allow the user to open the resulting page in urllib2 .

Is there any way to get this mixed behavior we want (to have precise control over urllib2 with higher level webbrowser )?

PS: We thought about the possibility of retreiving by URL with urllib2 , saving its contents to a temporary file and opening the file with webbrowser - webbrowser . This is a slightly unpleasant solution, in which case we will have to deal with other issues, such as relative URLs. Is there a better solution?

+5
source share
3 answers

I do not know how you can open the result of a POST request in a web browser without saving the result to a file and not opening it.

How about an alternative approach and temporary storage of data on the server. Then the page can be opened in the browser using a simple id parameter, and the saved partially filled form will be displayed.

+5
source

You can 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 

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

+2
source

This is not the correct answer. but it works too

 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') 
0
source

Source: https://habr.com/ru/post/1388040/


All Articles