【问题标题】:python requests with redirection带有重定向的python请求
【发布时间】:2015-08-30 08:59:17
【问题描述】:

尝试在http://72.ru 站点上进行身份验证,注意到有一个重定向到https://loginka.ru/auth/。发现有 302 POST 与数据形式的普通凭据。从 Chrome 复制标头可以在 cURL 中重现,但仍然无法在请求模块中访问。

警告:页面全是俄文字母,请在东北方的邮箱中注册

with requests.Session() as s:
    s.auth = ('EMAIL', 'PASSWD')

    s.post('http://72.ru/passport/login.php')
    p = s.get('http://72.ru/job/favorite/vacancy/')

    # will print True if logged
    print('some title from favorite page, if logged' in p.text)

为什么不能认证,我做错了什么?

【问题讨论】:

    标签: python authentication redirect curl python-requests


    【解决方案1】:

    我觉得你需要指定allow_redirects=True

    s.post('http://72.ru/passport/login.php', allow_redirects=True)
    

    【讨论】:

    • **kwargs 传递给底层 requests.request 调用的可选参数
    • 即使使用 allow_redirects=True =( 仍然没有变化
    • 兄弟,它现在在某个地方帮助了我,服务器可能正在重定向以创建隐藏的服务器变量。谢谢!
    【解决方案2】:
    from calendar import timegm
    from time import gmtime
    
    import requests
    
    headers = {
        "User-Agent":
            "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36",
    }
    
    s = requests.session()
    s.headers.update(headers)
    epoch1 = '%s000' % timegm(gmtime())
    s.get('http://72.ru/')
    epoch2 = '%s000' % timegm(gmtime())
    login_url = 'https://loginka.ru/service/api/passport/auth/token/?callback=jQuery17107978048569057137_%s&_=%s' % (epoch1, epoch2)
    s.get(login_url)
    epoch3 = '%s000' % timegm(gmtime())
    params = {
        'callback': 'jQuery17107978048569057137_%s' % epoch1,
        'email': username,     # Username Email
        'password': password,     # Password
        'remember': 0,
        '_': epoch3
    }
    r = self.s.get('https://loginka.ru/service/api/passport/auth/login/', params=params)
    print r.content
    

    【讨论】:

    • 很好,但是 jQuery 回调每次都会改变,无法找到如何确定行为,login_url 中的每个 epoch1 也不同。所以仍然无法得到 {'auth':'true'} 响应。
    • 我刚刚编辑了代码。现在使用 3 个 epoch。除此之外,我认为jquery之后的大数字应该没有问题,您可以使用我提供的静态数字。
    • 但是如何确定 ?callback=jQuery[NUMBER] 的数量?
    • 这就是我提到的,尝试在那里使用任何静态数字,希望它可以工作,你可以使用我的代码中的那个。我没有该站点的有效凭据,因此无法对其进行测试。
    • 我试了很多次,每次的数字都不一样。好的,谢谢,我再试试看。
    【解决方案3】:

    有一种更简单的方法可以登录本网站。

    import requests
    
    headers = {
        "User-Agent":
            "Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36",
    }
    
    s = requests.session()
    s.headers.update(headers)
    
    # There is a dedicated login page, which is the url of the Login button on the site, you can open that directly. 
    # Requests will automatically take care of rediects
    s.get('https://loginka.ru/auth/?url=http%3A%2F%2F72.ru')
    
    # Generate the post data
    data = {
        'url': 'http://72.ru',
        'email': username,
        'password': password
    }
    
    # Perform the post request
    r = s.post('https://loginka.ru/auth/?url=http%3A%2F%2F72.ru', data=data)
    
    # There is an extra post request on this site which uses token from redirect url
    token = r.url[r.url.index('token=')+6:]
    url = 'http://72.ru/put_token_to_user/?token=' + token + '&dummy_put_token_to_user=yes'
    headers2 = {'X-Requested-With': 'XMLHttpRequest', 'Referer': r.url}
    r = s.get(url, headers=headers2)
    
    r = s.get('http://72.ru/passport/mypage.php')
    print r.url
    print r.status_code
    with open('abc.txt', 'wb') as f:
        f.write(r.content)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-08-09
      • 2016-09-17
      • 2014-01-25
      • 1970-01-01
      • 2021-09-19
      • 2016-10-27
      • 2016-12-13
      • 2016-01-05
      相关资源
      最近更新 更多