V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
imn1
V2EX  ›  Python

这个命令用 requests 怎么写?

  •  
  •   imn1 · 2020-12-06 18:24:26 +08:00 · 2964 次点击
    这是一个创建于 1244 天前的主题,其中的信息可能已经有所发展或是发生改变。
    curl -X PROPFIND -u login -H "Content-Type: text/xml" -H "Depth: 1" \
    --data "<propfind xmlns='DAV:'><prop><calendar-data xmlns='urn:ietf:params:xml:ns:caldav'/></prop></propfind>" \
    https://myserver/joe/home/

    谢了 🌺
    第 1 条附言  ·  2020-12-06 19:18:37 +08:00
    虽然 https://curl.trillworks.com/ 给了个错误答案
    不过受启发后,照着 requests API 改了,可以了(抄抄改改还是做得到的,直接写就有点为难,咳咳)

    headers = {'Content-Type': 'text/xml', 'Depth': '1',}
    # data = '''<propfind xmlns="DAV:"><prop><calendar-data xmlns="urn:ietf:params:xml:ns:caldav"/></prop></propfind>'''.encode()
    # 上面这行用于 caldav
    data = '''<propfind xmlns="DAV:"><prop><address-data xmlns="urn:ietf:params:xml:ns:carddav"/></prop></propfind>'''.encode()
    req = requests.Request('propfind',
    carddav_url,
    headers=headers, data=data, auth=('user', 'pwd'))
    r = req.prepare()
    rs = requests.Session()
    print(rs.send(r).content.decode())
    第 2 条附言  ·  2020-12-06 19:44:48 +08:00

    data=b'''... 就可以了,不用 encode()那么繁琐
    8 条回复    2020-12-07 01:41:23 +08:00
    aonshuy
        1
    aonshuy  
       2020-12-06 18:31:41 +08:00   ❤️ 5
    imn1
        2
    imn1  
    OP
       2020-12-06 18:35:07 +08:00
    @aonshuy
    这年头真是只有想不到的…… 🌻
    justin2018
        3
    justin2018  
       2020-12-06 18:36:06 +08:00
    https://curl.trillworks.com/

    import requests

    headers = {
    'Content-Type': 'text/xml',
    'Depth': '1',
    }

    data = {
    '<propfind xmlns': '\'DAV:\'><prop><calendar-data xmlns=\'urn:ietf:params:xml:ns:caldav\'/></prop></propfind>'
    }

    response = requests.post('https://myserver/joe/home/', headers=headers, data=data, auth=('login', ''))
    ysc3839
        4
    ysc3839  
       2020-12-06 18:37:12 +08:00 via Android
    @aonshuy @justin2018
    这个网站转出来似乎是错的,忽略了 PROPFIND method 。
    no1xsyzy
        5
    no1xsyzy  
       2020-12-06 18:54:11 +08:00   ❤️ 3
    @ysc3839 去提个 issue 呗
    临时解决的话,应该还是用 requests.request('PROPFIND', ...)
    除此以外,直接 PyPI 上找找 WebDAV 库呗
    imn1
        6
    imn1  
    OP
       2020-12-06 19:27:27 +08:00
    @no1xsyzy #5
    没搜到 py3 可用的 carddav,caldav 那个还好,只是它仅有 caldav,代码看得有点晕,想继成过来兼容 carddav 没搞定
    只是我需求没那么复杂,自用不需要理会各种 rfc 标准,分析出字段能读能写就行,requests + vobject 就够了
    vone
        7
    vone  
       2020-12-06 23:40:16 +08:00   ❤️ 1
    你命令里的 "-u login" 不知道为什么被 postman 忽略了,改为 "-u login:",然后导入 postman,然后生成代码如下:

    import requests

    url = "https://myserver/joe/home/"

    payload="<propfind xmlns='DAV:'><prop><calendar-data xmlns='urn:ietf:params:xml:ns:caldav'/></prop></propfind>"
    headers = {
    'Content-Type': 'text/xml',
    'Depth': '1',
    'Authorization': 'Basic bG9naW46'
    }

    response = requests.request("PROPFIND", url, headers=headers, data=payload)

    print(response.text)


    postman 导入方式:主界面=> Import => Raw text => Continue 。

    https://i.loli.net/2020/12/06/5gvcKAYo1qEhrHR.jpg
    https://i.loli.net/2020/12/06/9m4abLBcCuysv1W.jpg
    WordTian
        8
    WordTian  
       2020-12-07 01:41:23 +08:00 via Android
    WEBDAV 是 HTTP 的协议扩展,requests 是 http 的库,理论上应该不支持的吧
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2242 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 05:46 · PVG 13:46 · LAX 22:46 · JFK 01:46
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.