如何通过python发送带有标头的GET请求

时间:2019-03-19 11:19:12

标签: python get fiddler

我有提琴手来捕获GET请求,我想用python重新发送确切的请求。 这是我捕获的请求:

GET https://example.com/api/content/v1/products/search?page=20&page_size=25&q=&type=image HTTP/1.1
Host: example.com
Connection: keep-alive
Search-Version: v3
Accept: application/json
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36
Referer: https://example.com/search/?q=&type=image&page=20
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9

3 个答案:

答案 0 :(得分:1)

您可以将SSL套接字(https://docs.python.org/3/library/ssl.html)打开到example.com:443,将捕获的请求作为原始字节写入此套接字,然后从套接字读取HTTP响应。

您还可以尝试使用http.client.HTTPResponse类从套接字读取和解析HTTP响应,但是不应直接实例化该类,因此可能会出现一些意外的障碍。

答案 1 :(得分:1)

import requests

headers = {
    'authority': 'stackoverflow.com',
    'cache-control': 'max-age=0',
    'upgrade-insecure-requests': '1',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36',
    'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
    'referer': 'https://stackoverflow.com/questions/tagged/python?sort=newest&page=2&pagesize=15',
    'accept-encoding': 'gzip, deflate, br',
    'accept-language': 'en-US,en;q=0.9,tr-TR;q=0.8,tr;q=0.7',
    'cookie': 'prov=6bb44cc9-dfe4-1b95-a65d-5250b3b4c9fb; _ga=GA1.2.1363624981.1550767314; __qca=P0-1074700243-1550767314392; notice-ctt=4%3B1550784035760; _gid=GA1.2.1415061800.1552935051; acct=t=4CnQ70qSwPMzOe6jigQlAR28TSW%2fMxzx&s=32zlYt1%2b3TBwWVaCHxH%2bl5aDhLjmq4Xr',
}

response = requests.get('https://stackoverflow.com/questions/55239787/how-to-send-a-get-request-with-headers-via-python', headers=headers)

这是一个如何向带有标题的页面发送获取请求的示例。

答案 2 :(得分:1)

您可以使用requests模块。

requests模块会自动为您提供大多数标题,因此您很可能不需要手动包含所有标题。

由于发送的是GET请求,因此可以使用params参数整齐地形成query string

示例:

import requests

BASE_URL = "https://example.com/api/content/v1/products/search"

headers = {
    "Connection": "keep-alive",
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36"
}

params = {
    "page": 20,
    "page_size": 25,
    "type": "image"
}

response = requests.get(BASE_URL, headers=headers, params=params)