Python 为什么我的请求会话过期?

Python 为什么我的请求会话过期?,python,session,cookies,python-requests,persistence,Python,Session,Cookies,Python Requests,Persistence,我正在使用Python 2.7和Django 1.10中的requests 2.12.1库来使用web服务。当我使用会话保存Cookie并使用持久性,并且在不使用任何web服务的情况下超过10秒时,我的视图将重新生成对象请求。会话() 这使得web服务不能为我服务,因为我的视图已经改变了cookies 这是my Views.py: client_session = requests.Session() @watch_login def loginUI(request): res

我正在使用Python 2.7和Django 1.10中的requests 2.12.1库来使用web服务。当我使用会话保存Cookie并使用持久性,并且在不使用任何web服务的情况下超过10秒时,我的视图将重新生成对象请求。会话()

这使得web服务不能为我服务,因为我的视图已经改变了cookies

这是my Views.py:

client_session = requests.Session()

@watch_login
def loginUI(request):
        response        =   client_session.post(URL_API+'login/', data={'username': username, 'password': password,})
        json_login      =   response.json()

@login_required(login_url="/login/")
def home(request):
  response_statistics = client_session.get(URL_API+'statistics/')
  log('ClientSession: '+str(client_session))

  try:
      json_statistics     =   response_statistics.json()
  except ValueError:
      log('ExcepcionClientSession: '+str(client_session))

      return logoutUI(request)

  return render(request, "UI/home.html", {
      'phone_calls'           :   json_statistics['phone_calls'],
      'mobile_calls'          :   json_statistics['mobile_calls'],
      'other_calls'           :   json_statistics['other_calls'],
      'top_called_phones'     :   json_statistics['top_called_phones'],
      'call_ranges_week'      :   json_statistics['call_ranges_week'],
      'call_ranges_weekend'   :   json_statistics['call_ranges_weekend'],
      'access_data'           :   accessData(request.user.username),
  })

  def userFeaturesFormInit(clientRequest):
    log('FeaturesClientSession: '+str(client_session))
    response = client_session.get(URL_API+'features/')

    try:
        json_features   =   response.json()
    except ValueError as e:
        log('ExcepcionFeaturesClientSession: '+str(client_session))
        raise e

谢谢。

我手动指定cookies并将其保存在请求中,修复了它

client_session = requests.Session()
response = client_session.post(URL_API+'login/', {'username': username, 'password': password,})
request.session['cookiecsrf']      =   client_session.cookies['csrftoken']
request.session['cookiesession']   =   client_session.cookies['sessionid']
并将其发送到gets/POST:

cookies = {'csrftoken' : request.session['cookiecsrf'], 'sessionid': request.session['cookiesession']}
response = requests.get(URL, cookies=cookies)

我修复了手动指定cookies并将其保存在请求中的问题

client_session = requests.Session()
response = client_session.post(URL_API+'login/', {'username': username, 'password': password,})
request.session['cookiecsrf']      =   client_session.cookies['csrftoken']
request.session['cookiesession']   =   client_session.cookies['sessionid']
并将其发送到gets/POST:

cookies = {'csrftoken' : request.session['cookiecsrf'], 'sessionid': request.session['cookiesession']}
response = requests.get(URL, cookies=cookies)

你在用django吗?是的,我在用django。你在用django吗?是的,我在用django。