python requests: show response in browser - python-requests

I am trying to open a page and get some data from it, using Requests and BeautifulSoup. How can I see the response of a requests.post in my browser as a page instead of the source code?

For a fast solution I use:
print(response.text)
I copy that text from console and paste it to .txt file,
Then I rename the .txt file to .html file
In windows I do that with:
ren file.txt file.html
Now I open this file with a browser. You can drag and drop the file icon into an open browser.
You also can write the response directly to an html file with this pseudo-code:
with open('file.html',mode='w') as file:
for line in response.text:
file.write(line)
now you don't have to manually rename the file each time to an html, is already there ready to use.

You need to learn about python GUI libraries like PyQt5 and instale it under your python libraries so that through QtWebEngineWidget and QWebView you can render your page to display it.

Related

downloading from a url with redirects

I am trying to download a files (it could be png, csv or pdf) from an internal url which seems to have redirects. I have tried downloading using download.file with extra=-L option, the download function from downloader package and the httr package.
However, in all cases I get a file of 768 B. Saving this file as a .txt shows that there is a another url within that. I have tried using that url, but without success. I see the following message (along with other information) in the downloaded file
Since your browser does not support JavaScript, you must press the Resume button once to proceed.
What works is if I stick that url in the browseURL function, I get a prompt to save the desired file.
I need to run the script in batch mode for reproducibility purposes, is there any way to run the browseURL in batch mode? or is there any other tool that would be useful here? (I have tried read.csv, fread etc. without any success). Unfortunately, I can't share the url as it is internal to my organization.
Thanks

Upload any file to ipython notebook directory

I have the following line on the notebook home page:
To import a notebook, drag the file onto the listing below or click here.
When I upload a text or any other file, I get an error message:
Invalid file type
Uploaded notebooks must be .ipynb files
How do I make it possible to upload any file ?
Is there any setting/ add on that will allow this?
This is now implemented on master (futur 3.0), we now have the content webservice which can deal with non-ipynb file. Just drag file onto the dashboard and it works(tm).
Keep in mind that this upload on the machine were the webserver is, not the machines were the kernels run in case they are not the same.

How to display a XDP pdf file in browser like how normal PDF file open?

I have a XDP file that Acrobat can open. It contains a PDF plus some data to fill in the form fields in the PDF.
In my HTTP request header I set
content-type to "application/vnd.adobe.xdp+xml" and
Content-Disposition to "in-line"
still my browser downloads the file and asks me if I want to Open it or save it. When I click on Open it loads the PDF form and data inside Acrobat, not in the web browser.
Do you have any idea?
UPDATE:
I reinstalled the Adobe plugin and now browser(FireFox) attempts to open the PDF but I get the following error:
that is very strange as the file is created by Acrobat it self. On the same page that throw this error, if I press OK and go to File > Save as.. and save the XDP file on desktop then drag and drop it into a new browser window, it opens it! and the address bar shows a very similar file name and location as was shown in the error (temp directory of FireFox cache).
It looks like an administration problem. Your file is being downloaded but Adobe doesn't have the access rights to that folder.
Contact your network administrator is my only answer.
I wonder if this open source project will solve your web display problem: pdf.js If not immediately, perhaps you can help it along.

browser Does not open 'Save file' dialog box

I have uploaded a file on server and given a link to that file (test.txt file say) in my asp.net page. when i click on this link, it opens the content of that text file within the browser. But i want to open a dialog box so that user can save it on his local machine.
More over if file is dll type then it gives 404 error(file not found).
I tried it out in IE and Firefox and also its working fine on my local IIS but not on Server machine.
There are various ways suggested of forcing a file download to the browser in ASP.Net, and lots of examples ("asp.net file download"). Here's one: http://aspalliance.com/259
Basically, what you'l need to do is transfer the file to the browser 'in code' using Response.WriteFile() or something similar.
You'll also need to set a couple of response headers so that the browser knows this is a download as opposed to a file it should try and load: Content-Type and Content-Disposition
Personally, I'd rather zip a DLL if it is to be downloaded - after all a DLL is executable code, and could be blocked at the client side.

how can we prevent .exe type file upload in a website?

Suppose we have a example.exe file.
we first put that file in a new folder
and then zip that folder with any zipping software,
Can we prevent that zipped folder upload in a website?
how can we do that?
You cannot prevent it, because you can't tell what the browser is going to submit before it submits it. All you can do is when the file arrives on the server, check the file extension - if it's an exe (or a .zip and you open it up and find an .exe) then reject it.
You can use something like SWFupload to get a handle on the file before it's uploaded, but the best that'll do is tell you the name of the file.
Besides, they could just take "example.exe" change the name to "example.txt" and still upload it...
You check on the server. Checking with javascript in form.onsubmit is dumb because its quite simple to post a form with a file to the same URL and skip your super secure javascript powered page.

Resources