Python x www form urlencoded request

Python Requests Post

Будет выполнять простую операцию HTTP POST. Отправленные данные могут быть в большинстве форматов, однако пары ключ-значение являются наиболее распространенными.

Заголовки можно посмотреть:

Заголовки также могут быть подготовлены перед публикацией:

 headers = foo = post('http://httpbin.org/post', headers=headers, data = ) 

кодирование

Кодировка может быть установлена ​​и просмотрена примерно так же:

 print(foo.encoding) 'utf-8' foo.encoding = 'ISO-8859-1' 

Проверка SSL

Запросы по умолчанию проверяют сертификаты SSL доменов. Это может быть отменено:

 foo = post('http://httpbin.org/post', data = , verify=False) 

Перенаправление

Будет выполнено любое перенаправление (например, http на https), это также можно изменить:

 foo = post('http://httpbin.org/post', data = , allow_redirects=False) 

Если после операции была перенаправлена, это значение может быть доступно:

Полная история перенаправлений может быть просмотрена:

Форма закодированных данных

 from requests import post payload = foo = post('http://httpbin.org/post', data=payload) 

Для передачи закодированных данных формы с помощью операции post данные должны быть структурированы как словарь и предоставлены в качестве параметра данных.

Если данные не хотят кодироваться формой, просто передайте строку или целое число в параметр данных.

Укажите в словаре параметр json для запросов на автоматическое форматирование данных:

 from requests import post payload = foo = post('http://httpbin.org/post', json=payload) 

Файл загружен

С помощью модуля запросов, его необходимо предоставить только дескриптор файла , в отличии от содержимого , извлекаемого с .read() :

 from requests import post files = foo = post('http://http.org/post', files=files) 

Имя файла, content_type и заголовки также могут быть установлены:

 files = <'file':('report.xls', open('report.xls', 'rb'), 'application/vnd.ms-excel', <'Expires': '0'>)> foo = requests.post('http://httpbin.org/post', files=files) 

Строки также могут быть отправлены в виде файла, если они поставляются как files параметров.

Несколько файлов

Несколько файлов могут быть предоставлены так же, как один файл:

 multiple_files = [ ('images',('foo.png', open('foo.png', 'rb'), 'image/png')), ('images',('bar.png', open('bar.png', 'rb'), 'image/png'))] foo = post('http://httpbin.org/post', files=multiple_files) 

Ответы

Коды ответов можно посмотреть из почтовой операции:

 from requests import post foo = post('http://httpbin.org/post', data=) print(foo.status_code) 

Возвращенные данные

Доступ к данным, которые возвращаются:

 foo = post('http://httpbin.org/post', data=) print(foo.text) 

Необработанные ответы

В тех случаях, когда вам необходим доступ к базовому объекту urllib3 response.HTTPResponse, это можно сделать следующим образом:

 foo = post('http://httpbin.org/post', data=) res = foo.raw print(res.read()) 

Аутентификация

Простая HTTP-аутентификация

Простая HTTP-аутентификация может быть достигнута с помощью следующего:

 from requests import post foo = post('http://natas0.natas.labs.overthewire.org', auth=('natas0', 'natas0')) 

Технически это короткая рука для следующего:

 from requests import post from requests.auth import HTTPBasicAuth foo = post('http://natas0.natas.labs.overthewire.org', auth=HTTPBasicAuth('natas0', 'natas0')) 

Дайджест-аутентификация HTTP

Проверка подлинности дайджеста HTTP выполняется очень похожим способом, для этого Requests предоставляет другой объект:

 from requests import post from requests.auth import HTTPDigestAuth foo = post('http://natas0.natas.labs.overthewire.org', auth=HTTPDigestAuth('natas0', 'natas0')) 

Пользовательская аутентификация

В некоторых случаях встроенных механизмов аутентификации может быть недостаточно, представьте этот пример:

Сервер настроен на прием аутентификации, если отправитель имеет правильную строку агента пользователя, определенное значение заголовка и предоставляет правильные учетные данные через HTTP Basic Authentication. Для достижения этого должен быть подготовлен пользовательский класс аутентификации, подклассифицирующий AuthBase, который является основой для реализаций аутентификации запросов:

 from requests.auth import AuthBase from requests.auth import _basic_auth_str from requests._internal_utils import to_native_string class CustomAuth(AuthBase): def __init__(self, secret_header, user_agent , username, password): # setup any auth-related data here self.secret_header = secret_header self.user_agent = user_agent self.username = username self.password = password def __call__(self, r): # modify and return the request r.headers['X-Secret'] = self.secret_header r.headers['User-Agent'] = self.user_agent r.headers['Authorization'] = _basic_auth_str(self.username, self.password) return r 

Это можно затем использовать с помощью следующего кода:

 foo = get('http://test.com/admin', auth=CustomAuth('SecretHeader', 'CustomUserAgent', 'user', 'password' )) 

Доверенные

Каждая операция POST запроса может быть настроена на использование сетевых прокси

HTTP / S Прокси

 from requests import post proxies = < 'http': 'http://192.168.0.128:3128', 'https': 'http://192.168.0.127:1080', >foo = requests.post('http://httpbin.org/post', proxies=proxies) 

Базовая аутентификация HTTP может быть предоставлена ​​следующим образом:

 proxies = foo = requests.post('http://httpbin.org/post', proxies=proxies) 

НОСКИ Прокси

Использование прокси — сервера SOCKS требует 3 — й партии зависимостей requests[socks] после того , как установлены носки прокси используются в очень похожим образом HTTPBasicAuth:

 proxies = < 'http': 'socks5://user:pass@host:port', 'https': 'socks5://user:pass@host:port' >foo = requests.post('http://httpbin.org/post', proxies=proxies) 

Источник

How to send a POST with Python Requests?

A POST request is a particular type of HTTP method used when we send data to services on the web. We use them on web sites that use forms — when we login, when we send messages or post an image. Applications also use POST requests to interact with other services to send data with JSON being a common data format for exchange.

The Requests library is one of the most popular HTTP client libraries for Python. It currently has over 45k stars on Github, with downloads on PyPI of 115M a month! It makes sending POST requests much simpler programmatically than having to send data via a headless browser. With a headless browser we’d need to write scripts to navigate to a site, move between fields, populate each of them with the desired data before finally submitting the data. By sending a POST request, we skip straight to the final submission step. Requests also is a much, much smaller library than a browser resulting in better performance and memory usage.

In this article we’ll cover how to construct a POST request using Requests and how it can make the process much simpler for us.

Building a JSON POST Request with Requests

As an example, lets start by building a JSON POST request the hard way. Don’t worry Requests will simplify this for us later! We’re using the httpbin.org service, which returns a JSON response detailing the content that was sent.

1. Set the Request Method to POST

Requests has a really simple HTTP verb based design, meaning that to get a response we call it’s .post() method supplying our URI as an argument. It also provides similar methods for GET, PUT and PATCH requests.

import requests r = requests.post("https://httpbin.org/post") 

2. Set the POST data

To actually send some data, we supply a data argument. By default data is sent using as a HTML form as we would get by submitting any form on the web. Requests sets the content type to ‘application/x-www-form-urlencoded’, so it isn’t necessary to set any headers.

import requests import json r = requests.post("https://httpbin.org/post", data="key": "value">, ) 

Inspecting the response from the httpbin.org service, we can see what was sent as form data under the «form» key.

>>> r.text '\n "args": <>, \n "data": "", \n "files": <>, \n "form": \n "key": "value"\n >, \n "headers": \n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Content-Length": "9", \n "Content-Type": "application/x-www-form-urlencoded", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.25.1", \n "X-Amzn-Trace-Id": "Root=1-60df1a04-0384d3ce7d9ac00b5855064b"\n >, \n "json": null, \n "origin": "**.***.**.***", \n "url": "https://httpbin.org/post"\n>\n' 

If we want to send JSON, we can supply a JSON formatted string. In our example, we’re using the json module to convert a dictionary to JSON formatted string ready to send.

import requests import json r = requests.post("https://httpbin.org/post", data=json.dumps("key": "value">), ) 

3. Set the POST Headers

Our JSON example isn’t going to work as it is now. With many services, we’ll likely get a 400 (Bad Request) HTTP status code. To prevent this we also need to inform the service we’re calling that our data is JSON so it can be handled correctly. To do so, we set the ‘Content-Type’ to ‘application/json’ in the request headers:

import requests import json r = requests.post( "https://httpbin.org/post", data=json.dumps("key": "value">), headers="Content-Type": "application/json">, ) 

4. POST JSON Data

If you think our JSON examples so far look a bit complex — you’re totally right. Requests makes it very easy to reduce all this down to a much simpler call. We can just supply a ‘json’ argument with our data. Requests will correctly set our headers and encode the JSON formatted string for us automatically.

import requests r = requests.post('https://httpbin.org/post', json='key':'value'>) 

Reading JSON Responses

By inspecting the response from our service, we can see the data returned is a JSON formatted string too. This is only text, so we’d need to parse it ourselves to use it within our Python script.

>>> r.text '\n "args": <>, \n "data": "\\"key\\": \\"value\\">", \n "files": <>, \n "form": <>, \n "headers": \n "Accept": "*/*", \n "Accept-Encoding": "gzip, deflate", \n "Content-Length": "16", \n "Content-Type": "application/json", \n "Host": "httpbin.org", \n "User-Agent": "python-requests/2.25.1", \n "X-Amzn-Trace-Id": "Root=1-60df0aaa-3105fc35436042571335fa22"\n >, \n "json": \n "key": "value"\n >, \n "origin": "**.***.**.***", \n "url": "https://httpbin.org/post"\n>\n' 

Alternatively, if we want to access the response as a JSON object we can also use Requests built in JSON decoder by calling .json() on the response object.

>>> r.json() 'args': <>, 'data': '', 'files': <>, 'form': <>, 'headers': 'Accept': '*/*', 'Accept-Encoding': 'gzip, deflate', 'Content-Length': '16', 'Content-Type': 'application/json', 'Host': 'httpbin.org', 'User-Agent': 'python-requests/2.25.1', 'X-Amzn-Trace-Id': 'Root=1-60deee08-4e9c76b6457826d5001b76fa'>, 'json': 'key': 'value'>, 'origin': '**.***.**.***', 'url': 'http://httpbin.org/post'> 

Making POST requests within a Session

As a final example, lets login to Hacker News using Requests and submit some form data to login in combination with a requests.Session() object. Any cookies we receive will be stored in the object, and will be reused in later .get() calls to the session.

For our example, we’ll search the response text of the news page to confirm if we’re shown the logout link indicating we’re being served a logged in page.

import requests session = requests.Session() r = session.post("https://news.ycombinator.com/login", data=< "acct": "username", "pw": "***not-telling***" >) r = session.get("https://news.ycombinator.com/news") logout_pos = r.text.find("logout") print(r.text[logout_pos-20:logout_pos+20]) # 

Great! Our link is in the response indicating we’re logged in. We can now continue to interact with the site using the session object via Requests.

Conclusion

You’ve seen just how simple Requests makes it to post data to sites or services and how we can reduce much of the common code in our applications by using it. This simple design has certainly contributed to it’s success in the Python community, making it a firm favorite with developers. Before you reach for a full blown headless browser to send your data, you should definitely consider using Requests.

You will often need proxies for your web scraping projects, don’t hesitate to checkout this article on how to use proxies with Python Requests.

image description

Ian is a freelance developer with a passion for simple solutions. He has written code to power surveys, studio pipelines and holds a PhD in distributed computing.

Источник

Читайте также:  Set to array kotlin
Оцените статью