import requests
send = requests.head('https://httpbin.org')
print(send.text)
What does 'requests.head()' do ? i get empty responses
A head request is a request used when you do not actually need the page content - just the status-code of your connection with the site or http-headers.
Related
Like we open a URL to a normal browser so it will redirect to another website url. Example a shorted link. After you open this it will redirect you to the main url.
So how to do this in python I mean I need to open a URL on python and this will redirect to other website page then I will copy the other website page link.
That's all I want to know thank you.
I tried it with python requests and urllib module.
Like this
import requests
a = requests.get("url", allow_redirects = True)
And
import urllib.request
a = urllib.request.urlopen("url")
But it's not working at all. I mean didn't get the redirected page.
I know 4 types of redirections.
server sends response with status 3xx and new address
HTTP/1.1 302 Found
Location: https://new_domain.com/some/folder
Wikipedia: HTTP 301, HTTP 302, HTTP 303
server sends header Refresh with time in seconds and new address
Refresh: 0; url=https://new_domain.com/some/folder
server sends HTML with meta tag which emulates header Refresh
<meta http-equiv="refresh" content="0; url=https://new_domain.com/some/folder">
Wikipedia: meta refresh
JavaScript sets new location
location = url
location.href = url
location.replace(url)
location.assing(url)
The same for document.location, window.location
There should be also combination with open(),document.open(), window.open()
requests automatically redirects for first and (probably) second type. With urllib probably you would have to check status, get url, and run next request - but this is easy. You can even run it in loop because some pages may have many redirections. You can test it on httpbin.org (even for multi-redirections)
For third type it is easy to check if HTML has meta tag and run next request with new url. And again you can run in loop because some pages may have many redirections.
But forth type makes problem because requests can't run JavaScript and there are many different methods to assign new location. They can also hide it in code - "obfuscation".
In requests you can check response.history to see executed redirections
I want to create a POST request to a site like mydomain.com?character=%61 using Requests library.
But if I use requests.post(url) with url = 'mydomain.com?character=%61' then it sends a POST request to mydomain.com?character=a. How can I keep %61 in request instead of decoding it to a?
Thanks all!
I am writing a small application that interprets the http response of a request. I am writing the application in python. I have not found anything that allows me to send the body + headers stored in one file. I can send certain parts like the headers but not the entire request.
For example, if the request is:
GET /index.html HTTP/1.1
Host: localhost
Cookie: bob=lemon
I want to send this entire request in one go. How would I do this in python?
Check out the python requests library. https://requests.readthedocs.io/en/master/user/quickstart/#make-a-request
For the request above it would look something like
import requests
url = 'http://localhost:[YOUR PORT HERE]/'
cookies = {bob : lemon}
r = requests.get(url, cookies=cookies)
To check if you had a successful request you should get a 200 code from.
r.status_code
Check out the library for more, it is very extensive.
I need to get some json data using API
import requests
url = 'https://example.com/api/some-info/'
response = requests.get(url)
print(response.text) # Here is JSON needeed
And everything fine, except I need to make such requests very often, and API provider says:
You'll be banned if you make more than 5 requests per second, so use
sockets
So, how can I make this work via sockets?
Big thx for advices.
I am doing a Scan work,and Need to send http requests without urlencode. The web application use $_SERVER['REQUEST_URI'](php) to get URL, and I must use "<" or ">" to do my scan work. But python requests will encode ">" to "%3E". Is there any way to send http request without urlencode(requests, urllib, urllib2), except sending http requests using socket?
Firstly, I use requests to do it:
import requests
requests.get("http://test.com/index.php?id=1 and 1>1")
But the truth url the requests get is: http://test.com/index.php?id=1 and 1%3E1
This question has confused me for days, I'll be very happy to get any solution from you, and thanks all.
You can pass string parameters using the params argument. Like this:
requests.get("http://test.com/index.php", params="id=1 and 1>1")