session请求示例

2019/04/07 技术

session请求示例

1. requests session

requests自带session管理, 示例:

import json
import requests

with requests.Session() as session:
    session.get('https://httpbin.org/cookies/set/sessioncookie/123456789')
    r = session.get('https://httpbin.org/cookies')
    assert r.status_code == 200
    assert json.loads(r.text)["cookies"]["sessioncookie"] == "123456789"

2. scrapy session

scrapy使用cookiejar管理session. 参考.

def start_first_page(self, ):
   yield scrapy.Request("https://httpbin.org/cookies/set/sessioncookie/123456789", meta={'cookiejar': 0},
        callback=self.parse_second_page)
        
def parse_second_page(self, response):
    return scrapy.Request("https://httpbin.org/cookies",
        meta={'cookiejar': response.meta['cookiejar']},
        callback=self.parse_other_page)

3. tornado client + session

tornado本身不带session模块, 客户端可使用cookies维护session.

获取新cookies:

cookies = response.headers.get_list('Set-Cookie')

使用新cookies:

import tornado.httpclient

http_client = tornado.httpclient.HTTPClient()
# cookies = {"Cookie" : 'my_cookie=abc'}
http_client.fetch("http://abc.com/test", headers=cookies)

Search

    Table of Contents