Python 请求:会话中的 URL 基础 [英] Python requests: URL base in Session

查看:26
本文介绍了Python 请求:会话中的 URL 基础的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Session 时,似乎每次都需要提供完整的 URL,例如

When using a Session, it seems you need to provide the full URL each time, e.g.

session = requests.Session()
session.get('http://myserver/getstuff')
session.get('http://myserver/getstuff2')

这有点乏味.有没有办法做这样的事情:

This gets a little tedious. Is there a way to do something like:

session = requests.Session(url_base='http://myserver')
session.get('/getstuff')
session.get('/getstuff2')

推荐答案

requests_toolbelt.sessions.BaseUrlSessionhttps://github.com/requests/toolbelt/8ac8c8c8c8c8c5c8c8c8c8c5c8c8c5c8c8c8c8c5c8c8c8c8c8c2fd5c7cb3/requests_toolbelt/sessions.py#L6

注意:这使用了标准库中的 urljoin.注意 urljoin 的行为.

NOTE: This uses urljoin from standard lib. Beware of urljoin's behavior.

In [14]: from urlparse import urljoin

In [15]: urljoin('https://localhost/api', '/resource')
Out[15]: 'https://localhost/resource'

In [16]: urljoin('https://localhost/api', 'resource')
Out[16]: 'https://localhost/resource'

In [17]: urljoin('https://localhost/api/', '/resource')
Out[17]: 'https://localhost/resource'

In [18]: urljoin('https://localhost/api/', 'resource')
Out[18]: 'https://localhost/api/resource'

import requests 
from functools import partial

def PrefixUrlSession(prefix=None):                                                                                                                                                                                                                                                                                                                 
     if prefix is None:                                                                                                                                                                                                                                                                                                                             
         prefix = ""                                                                                                                                                                                                                                                                                                                                
     else:                                                                                                                                                                                                                                                                                                                                          
         prefix = prefix.rstrip('/') + '/'                                                                                                                                                                                                                                                                                                          

     def new_request(prefix, f, method, url, *args, **kwargs):                                                                                                                                                                                                                                                                                      
         return f(method, prefix + url, *args, **kwargs)                                                                                                                                                                                                                                                                                            

     s = requests.Session()                                                                                                                                                                                                                                                                                                                         
     s.request = partial(new_request, prefix, s.request)                                                                                                                                                                                                                                                                                            
     return s             

这篇关于Python 请求:会话中的 URL 基础的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆