Skip to content

Python请求HTTP

from utils.request import request_get_data

python
import requests
from requests_toolbelt import MultipartEncoder
import secrets
import string
from .cache import cache

DEFAULT_USER_AGENT = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/119.0.0.0 Safari/537.36 Edg/119.0.0.0'

def generate_token_hex(length=16):
    return ''.join(secrets.choice(string.ascii_letters + string.digits) for _ in range(length))

def print_request_info(method, url, payload, headers):
    print(f'[{method}] {url}')
    print(f'Payload: {payload}')
    print(f'Headers: {headers}')

@cache()
def request_data(method, url, data=None, headers=None, is_formdata=False):
    user_agent = headers.pop('User-Agent', DEFAULT_USER_AGENT) if headers else DEFAULT_USER_AGENT
    headers = {'User-Agent': user_agent, **headers} if headers else {'User-Agent': user_agent}

    if method == 'GET':
        response = requests.get(url, headers=headers)
    elif method == 'POST':
        if is_formdata:
            boundary = f'----WebKitFormBoundary{generate_token_hex()}'
            m = MultipartEncoder(fields=data, boundary=boundary)
            payload = m.to_string().decode()
            headers.update({'content-type': m.content_type})
        else:
            payload = data

        print_request_info(method, url, payload, headers)
        response = requests.post(url, data=payload, headers=headers)

    if response.status_code == 200:
        result_data = response.content
    else:
        result_data = b''

    print(f'[+] Got data, size: {len(result_data)}')
    return result_data

def request_html(method, url, data=None, headers=None, is_formdata=False):
    return request_data(method, url, data, headers=headers, is_formdata=is_formdata).decode()

# Example usage:
# html_data = request_html('GET', 'https://example.com')
# post_data = request_data('POST', 'https://example.com/api', data={'key': 'value'}, is_formdata=True)

Released under the MIT License.