使用requests.post提交URL遇405错误,求助向目标站点发送链接
First, let's unpack the 405 error: it means the server at http://csec380-core.csec.rit.edu:5004/ rejects the POST method for the root URL you're targeting. This usually happens for one of a few simple reasons—let's break down how to fix it:
Step 1: Inspect the Message Board's Form Details
To get the right request parameters, use your browser's developer tools to check the actual form on the page:
- Visit
http://csec380-core.csec.rit.edu:5004/in your browser. - Press F12 to open DevTools, then navigate to the Elements tab.
- Locate the
<form>element on the page and note three key details:action: The specific URL the form submits to (it might be something like/submitinstead of the root/).method: Whether the form usesPOSTorGET—this is the HTTP method you need to mirror in your code.- The
nameattribute of the message input/textarea (you usedmessage, but it could bemsg,content, or another label).
Step 2: Adjust Your Python Code
Based on what you found in DevTools, update your code to match the form's requirements. Here are common scenarios:
Scenario 1: Form submits to a dedicated endpoint
If the form's action points to something like /submit, update your request URL:
import requests # Use the correct endpoint from the form's action attribute url = 'http://csec380-core.csec.rit.edu:5004/submit' # Use the exact field name from the form's input data = {'message': 'http://52.14.44.145/'} r = requests.post(url, data=data) print(r.status_code) print(r.text)
Scenario 2: Form uses GET instead of POST
If the form's method is set to GET, switch to a GET request and pass your message as URL parameters:
import requests url = 'http://csec380-core.csec.rit.edu:5004/' params = {'message': 'http://52.14.44.145/'} r = requests.get(url, params=params) print(r.status_code) print(r.text)
Scenario 3: Add Browser-like Headers (If Needed)
Some servers block requests that don't mimic a real browser. Add a User-Agent header to your request:
import requests url = 'http://csec380-core.csec.rit.edu:5004/submit' data = {'message': 'http://52.14.44.145/'} headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36' } r = requests.post(url, data=data, headers=headers) print(r.status_code) print(r.text)
Quick Troubleshooting Tip
If you still hit errors, double-check for hidden input fields in the form (DevTools will show these)—some sites require a CSRF token or other hidden values to process submissions.
内容的提问来源于stack exchange,提问作者lul

