V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐工具
RoboMongo
推荐书目
50 Tips and Tricks for MongoDB Developers
Related Blogs
Snail in a Turtleneck
Ewig
V2EX  ›  MongoDB

Python 如何看某个脚本的状态

  •  
  •   Ewig · 2018-12-22 00:50:08 +08:00 · 5488 次点击
    这是一个创建于 2156 天前的主题,其中的信息可能已经有所发展或是发生改变。

    今天遇到一个问题,就是在 mongodb 修改 cookie 的值 因为 cookie 的值太长, 于是我这样

    db.coolection.update({"cookie":"kr_stat_uuid=s83pP25734456; download_animation=1; device-uid=9447e910-f90a-11e8-85f3-853e28e448cb; kr_plus_id=643809727; krnewsfrontss=e88ee1dafbc74062e5286b2ad050d1c2; M-XSRF-TOKEN=955cda13b9df340725a9f6ae46bc3dd467969129c4e71424c9723edcf4989b26; krnewsfrontcc=eyJ0eXAiOiJKV1QiLCJhbGciOiIzNmtyLWp3dCJ9.eyJpZCI6MTA5MjU0OTQ0NSwic2Vzc2lvbl9pZCI6ImU4OGVlMWRhZmJjNzQwNjJlNTI4NmIyYWQwNTBkMWMyIiwiZXhwaXJlX3RpbWUiOjE1NzY5MjAwOTB9.06bc5b3743abdd862451a739a13ffe0b52128c0e4d6dbcec251191814055700e; kr_plus_utype=0; Hm_lvt_1684191ccae0314c6254306a8333d090=1544420094,1544498178,1544514883,1545384091; Hm_lvt_713123c60a0e86982326bae1a51083e1=1544420094,1544498178,1544514883,1545384092; Hm_lvt_e8ec47088ed7458ec32cde3617b23ee3=1544067381,1545384189; kr_p_se=c797cd2a-c544-46f5-b480-95168f91d953; krid_user_id=643809727; krid_user_version=2; kr_plus_token=9hDrv3NZrGA3KX3GeFU963hC1IRE6gWmz5a____; Hm_lpvt_e8ec47088ed7458ec32cde3617b23ee3=1545384194; _ga=GA1.2.1819701447.1545384194; gid=GA1.2.2007305715.1545384194; gr_user_id=db313a76-07b8-4d53-b244-fc04f5648ba0; gr_session_id_76d36bd044527820a1787b198651e2f1=6577c323-76c4-4de0-937d-80dbde1021d5; gr_session_id_76d36bd044527820a1787b198651e2f1_6577c323-76c4-4de0-937d-80dbde1021d5=true; Z-XSRF-TOKEN=eyJpdiI6ImF1MEhXR09lcHRBQ2puTUJ1R1ltYkE9PSIsInZhbHVlIjoieGdrZWQzNThUdUcwVTZCVk1laW9MTG1pQXJPRnUrUnlSSXpiQ2Q4d1k3Vk9wQmI1YkhNNGdIM3VVZkZ3dVpsMGV2OUFJSldEck9ldWgxenNcL25KeU1nPT0iLCJtYWMiOiI4MThjMTI5ODIzZmYyNGVhOTJmMTQ5MGZlYWIxYzQ1YWZjNWJkYzRiMzkwMjU5MmVjMmY2NGIyMmE1MzA2NGYxIn0%3D; krchoasss=eyJpdiI6InAzdTZvQk5nYXFRdzE4WWc4cGpndnc9PSIsInZhbHVlIjoicGJuWXU2a21ZYjd4anA0bjF4azlqWEo2RUJINUxRYXRra0dON0JcL3VFZHJqbG8yVXBQeUFGcnZSeW40ZWxxbmNtTlwvSERwRVFrazduc2N6bGJjNkZGUT09IiwibWFjIjoiMmIyYzQ1ZjljZmI0MDU0N2NhMmE0NTQ5Y2IzZjMwNGQxZTZlMGQyOTJhYjMxNDk1MzQ3ODRiNTMyOWY5MGUyMiJ9; sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22643809727%22%2C%22%24device_id%22%3A%2216781a6b6eb731-02acab268c12f2-163b6953-1296000-16781a6b6ec18d%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E7%9B%B4%E6%8E%A5%E6%B5%81%E9%87%8F%22%2C%22%24latest_referrer%22%3A%22%22%2C%22%24latest_referrer_host%22%3A%22%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%E7%9B%B4%E6%8E%A5%E6%89%93%E5%BC%80%22%7D%2C%22first_id%22%3A%2216781a6b6eb731-02acab268c12f2-163b6953-1296000-16781a6b6ec18d%22%7D; Hm_lpvt_1684191ccae0314c6254306a8333d090=1545385052; Hm_lpvt_713123c60a0e86982326bae1a51083e1=1545385052"},{$set:{"cookie":"xxxx"}})

    我感觉这样复制 cookie 很不方便,求人指教,有没有简便方法更新 mongo 里面的某个 value 值?

    8 条回复    2018-12-22 10:25:01 +08:00
    Ops
        1
    Ops  
       2018-12-22 02:23:42 +08:00
    字符串加密再存
    yuikns
        2
    yuikns  
       2018-12-22 02:41:56 +08:00 via iPhone
    不太明白楼主的意思,是说想要只更新 cookie 某个 kv pair 么?

    可以考虑把 foo=bar; key=value 解析为 { foo: bar, key: value }

    这样存储到 mongo 里面可以 set cookie.foo 了吧
    Trim21
        3
    Trim21  
       2018-12-22 07:12:43 +08:00 via Android
    cookies 就是个 dict,像楼上这样做比较好
    Ewig
        4
    Ewig  
    OP
       2018-12-22 09:46:29 +08:00
    @Ops mongo 里面还能字符串加密?
    Ewig
        5
    Ewig  
    OP
       2018-12-22 09:47:41 +08:00
    @yuikns 没明白你的意思,你能写出来吗?谢谢
    yuikns
        6
    yuikns  
       2018-12-22 10:18:34 +08:00
    cookie_str = "kr_stat_uuid=s83pP25734456; download_animation=1"
    cookie = {}
    for kv in cookie_str.split(";"):
    pair = kv.strip().split("=")
    if len(pair) == 2:
    cookie[pair[0]] = pair[1]
    print(cookie['kr_stat_uuid']) # = s83pP25734456
    db.coolection.update({"cookie.kr_stat_uuid": cookie['kr_stat_uuid'] }, {$set: {'cookie': cookie}} )


    set 后面的 key 也可以是具体值。
    adrianyoung
        7
    adrianyoung  
       2018-12-22 10:20:45 +08:00
    考虑一下 pickle ?
    yuikns
        8
    yuikns  
       2018-12-22 10:25:01 +08:00
    因为 cookie 里面的 kv 内容的 = ; 都被转义过,简单 split 切一下就行。要是要用 concat 也很容易

    写到 mongo 里面字符串可以手动 aes 啊

    这样要是只是修改特定的 kv,那么可以


    db.coolection.update({"cookie.kr_stat_uuid": cookie['kr_stat_uuid'] }, {$set: {'cookie. download_animation': '0' }} )
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   1436 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 22ms · UTC 17:38 · PVG 01:38 · LAX 09:38 · JFK 12:38
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.