推荐学习书目
› 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 · Dec 6, 2020 · 3926 views
    This topic created in 2172 days ago, the information mentioned may be changed or developed.
    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/

    谢了 🌺
    Supplement 1  ·  Dec 6, 2020
    虽然 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())
    Supplement 2  ·  Dec 6, 2020
    晕
    data=b'''... 就可以了,不用 encode()那么繁琐
    8 replies  •  2020-12-07 01:41:23 +08:00
    imn1
    imn1
    OP
    Dec 6, 2020
    2
    @aonshuy
    这年头真是只有想不到的…… 🌻
    justin2018
    justin2018
    Dec 6, 2020
    3
    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
    ysc3839
    Dec 6, 2020 via Android
    4
    @aonshuy @justin2018
    这个网站转出来似乎是错的,忽略了 PROPFIND method 。
    no1xsyzy
    no1xsyzy
    Dec 6, 2020❤️3
    5
    @ysc3839 去提个 issue 呗
    临时解决的话,应该还是用 requests.request('PROPFIND', ...)
    除此以外,直接 PyPI 上找找 WebDAV 库呗
    imn1
    imn1
    OP
    Dec 6, 2020
    6
    @no1xsyzy #5
    没搜到 py3 可用的 carddav,caldav 那个还好,只是它仅有 caldav,代码看得有点晕,想继成过来兼容 carddav 没搞定
    只是我需求没那么复杂,自用不需要理会各种 rfc 标准,分析出字段能读能写就行,requests + vobject 就够了
    vone
    vone
    Dec 6, 2020❤️1
    7
    你命令里的 "-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
    WordTian
    Dec 7, 2020 via Android
    8
    WEBDAV 是 HTTP 的协议扩展,requests 是 http 的库,理论上应该不支持的吧
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   4211 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 31ms · UTC 09:16 · PVG 17:16 · LAX 01:16 · JFK 04:16
    ♥ Do have faith in what you're doing.