Python实现非常规请求方法的Requests测试可行性咨询
Great question! You absolutely can send non-standard HTTP request methods like "SSS" using Python, and while the requests module doesn't offer a direct, top-level function for this (unlike its built-in get(), post(), etc. methods), it's totally feasible by leveraging its lower-level components. Alternatively, you can use urllib3 (the HTTP library that powers requests) directly for even more control.
Option 1: Use requests with PreparedRequest
The requests library lets you construct a fully prepared request object where you can specify any custom method. Here's a working example:
import requests # Build a custom request with your non-standard method custom_request = requests.Request( method="SSS", url="http://your-target-server.com", headers={"Content-Type": "application/json"}, json={"test_key": "test_value"} # Add body data if your request needs it ) # Prepare the request (handles header formatting, body encoding, etc.) prepped_request = custom_request.prepare() # Send the prepared request using a Session with requests.Session() as session: response = session.send(prepped_request) print(f"Response Status Code: {response.status_code}") print(f"Response Content: {response.text}")
This approach bypasses the requests API's restrictions on standard methods. When you run this code, Wireshark will capture the request with the "SSS" method clearly visible in the HTTP header.
Option 2: Directly Use urllib3
Since requests is built on top of urllib3, you can use urllib3 directly to send custom method requests. This might feel more explicit if you don't need the extra convenience features requests provides:
import urllib3 # Create a PoolManager for efficient connection pooling http = urllib3.PoolManager() # Send the custom method request response = http.request( method="SSS", url="http://your-target-server.com", headers={"Content-Type": "application/json"}, body=b'{"test_key": "test_value"}' # Note: body must be passed as bytes ) print(f"Response Status Code: {response.status}") print(f"Response Content: {response.data.decode('utf-8')}")
Important Notes
- Many web servers are configured to only accept standard HTTP methods (GET, POST, PUT, DELETE, etc.). Sending a non-standard method like "SSS" might result in a
405 Method Not Allowedresponse unless the server is explicitly set up to handle it. - When using Wireshark, make sure you're using the right filter (e.g.,
http) to easily spot your custom method in the captured traffic.
内容的提问来源于stack exchange,提问作者Elias Shourosh

