这个命令用 requests 怎么写? - V2EX
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 3331 次点击
    这是一个创建于 1850 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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     Solana     1300 人在线   最高记录 6679       Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 25ms UTC 17:07 PVG 01:07 LAX 09:07 JFK 12:07
    Do have faith in what you're doing.
    ubao msn snddm index pchome yahoo rakuten mypaper meadowduck bidyahoo youbao zxmzxm asda bnvcg cvbfg dfscv mmhjk xxddc yybgb zznbn ccubao uaitu acv GXCV ET GDG YH FG BCVB FJFH CBRE CBC GDG ET54 WRWR RWER WREW WRWER RWER SDG EW SF DSFSF fbbs ubao fhd dfg ewr dg df ewwr ewwr et ruyut utut dfg fgd gdfgt etg dfgt dfgd ert4 gd fgg wr 235 wer3 we vsdf sdf gdf ert xcv sdf rwer hfd dfg cvb rwf afb dfh jgh bmn lgh rty gfds cxv xcv xcs vdas fdf fgd cv sdf tert sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf sdf shasha9178 shasha9178 shasha9178 shasha9178 shasha9178 liflif2 liflif2 liflif2 liflif2 liflif2 liblib3 liblib3 liblib3 liblib3 liblib3 zhazha444 zhazha444 zhazha444 zhazha444 zhazha444 dende5 dende denden denden2 denden21 fenfen9 fenf619 fen619 fenfe9 fe619 sdf sdf sdf sdf sdf zhazh90 zhazh0 zhaa50 zha90 zh590 zho zhoz zhozh zhozho zhozho2 lislis lls95 lili95 lils5 liss9 sdf0ty987 sdft876 sdft9876 sdf09876 sd0t9876 sdf0ty98 sdf0976 sdf0ty986 sdf0ty96 sdf0t76 sdf0876 df0ty98 sf0t876 sd0ty76 sdy76 sdf76 sdf0t76 sdf0ty9 sdf0ty98 sdf0ty987 sdf0ty98 sdf6676 sdf876 sd876 sd876 sdf6 sdf6 sdf9876 sdf0t sdf06 sdf0ty9776 sdf0ty9776 sdf0ty76 sdf8876 sdf0t sd6 sdf06 s688876 sd688 sdf86