Python中如何在Session中处理带重定向的登录页面?资源迁移后认证函数失效求助
Hey there, let's figure out why your authentication script isn't working with the new link and get it fixed. Here are the most likely issues and how to address them:
1. Fix the Typo in Your Request Headers
First off, I noticed a small but critical typo in your headers: you wrote 'acept' instead of 'Accept'. Servers are strict about header names, and this could be causing the server to return an unexpected response instead of following redirects properly. Correct that line first:
self.headers = {'user-agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:76.0) Gecko/20100101 Firefox/76.0', 'Accept': '*/*'}
2. Inspect the Full Redirect Chain Manually
The new link uses redirects that your requests session isn't handling correctly—even with allow_redirects=True. Let's break down the redirect steps to see where it's failing:
- Temporarily disable redirects and check the first response's
Locationheader to see where it's supposed to go next:response = self.session.get(new_link, allow_redirects=False) print("First redirect target:", response.headers.get('Location')) - Manually follow each redirect step by step (using your session to preserve cookies) and check the status code/content at each stage. This will help you spot if one of the redirects requires additional parameters, cookies, or a different request method.
3. Handle JavaScript-Driven Redirects
If the redirects on the new link are powered by JavaScript (e.g., window.location.href in the page source), the requests library won't execute that code—so it can't follow the redirect to the login form. In this case, you'll need to use a browser automation tool that can run JS, like Selenium or Playwright.
Here's a quick example using Selenium to simulate a real browser and reach the login page:
from selenium import webdriver from selenium.webdriver.firefox.options import Options from selenium.webdriver.common.by import By class ExampleClass(object): def __init__(self, login, password): self.login = login self.password = password self.driver = self.setup_driver() self.resource_auth() def setup_driver(self): options = Options() options.add_argument('--headless') # Run in headless mode for build machines driver = webdriver.Firefox(options=options) return driver def resource_auth(self): new_link = 'https://www.new_example.ru/auth/oid/new/?next=/' self.driver.get(new_link) # Now you can interact with the login form using Selenium methods username_field = self.driver.find_element(By.ID, 'username') password_field = self.driver.find_element(By.ID, 'password') username_field.send_keys(self.login) password_field.send_keys(self.password) # Submit the form (adjust selector based on actual button) self.driver.find_element(By.CSS_SELECTOR, 'button[type="submit"]').click()
4. Match Browser Request Headers Exactly
Your current headers are close, but real browsers send more detailed headers (like Accept-Language, Referer, Connection). Use your browser's DevTools (F12 > Network tab) to copy the full request headers from when you manually visit the new link, then paste them into your script. This ensures your request looks identical to a real browser's, which can bypass server-side checks that block automated requests.
5. Check for IP/Proxy Restrictions
Since your script runs on a build machine, it's possible the new host is blocking the build machine's IP address. Test running the script locally (if you can) to see if it works there. If it does, you'll need to:
- Contact the new host's admin to whitelist the build machine's IP
- Or configure a proxy in your
requestssession to route traffic through an allowed IP:proxies = { 'http': 'http://your-proxy:port', 'https': 'https://your-proxy:port', } response = self.session.get(new_link, proxies=proxies)
Start with the header typo and inspecting the redirect chain—those are the easiest fixes. If those don't work, move on to checking for JS redirects or IP restrictions.
内容的提问来源于stack exchange,提问作者swede

