reading cookies file created by curl











up vote
3
down vote

favorite
1












I have the following cookie saved by curl (in test.txt, tab-separated, this editor doesn't preserve tabs):



# Netscape HTTP Cookie File
# http://curlm.haxx.se/rfc/cookie_spec.html
# This file was generated by libcurl! Edit at your own risk.

#HttpOnly_my-example.com FALSE / FALSE 0 _rails-root_session test


I'm trying to read it with the following code:



import sys

if sys.version_info < (3,):
from cookielib import Cookie, MozillaCookieJar
else:
from http.cookiejar import Cookie, MozillaCookieJar

def load_cookies_from_mozilla(filename):
ns_cookiejar = MozillaCookieJar()
ns_cookiejar.load(filename, ignore_discard=True)
return ns_cookiejar

cookies = load_cookies_from_mozilla("test.txt")
print (len(cookies))


It outputs 0 (unable to read the cookie).
If I manually modify my cookie to the following line (remove HttpOnly flag and changing 0 to the empty string for expiration time, and again, tab-separated):



my-example.com  FALSE   /   FALSE       _rails-root_session test


then it outputs 1 (successfully read the cookie).



What needs to be done to my python code to read the original cookie line? And preferably to be able to save it in the same format (with HttpOnly flag and with 0 instead of empty string for never-expiring cookie)?



Thanks.










share|improve this question




























    up vote
    3
    down vote

    favorite
    1












    I have the following cookie saved by curl (in test.txt, tab-separated, this editor doesn't preserve tabs):



    # Netscape HTTP Cookie File
    # http://curlm.haxx.se/rfc/cookie_spec.html
    # This file was generated by libcurl! Edit at your own risk.

    #HttpOnly_my-example.com FALSE / FALSE 0 _rails-root_session test


    I'm trying to read it with the following code:



    import sys

    if sys.version_info < (3,):
    from cookielib import Cookie, MozillaCookieJar
    else:
    from http.cookiejar import Cookie, MozillaCookieJar

    def load_cookies_from_mozilla(filename):
    ns_cookiejar = MozillaCookieJar()
    ns_cookiejar.load(filename, ignore_discard=True)
    return ns_cookiejar

    cookies = load_cookies_from_mozilla("test.txt")
    print (len(cookies))


    It outputs 0 (unable to read the cookie).
    If I manually modify my cookie to the following line (remove HttpOnly flag and changing 0 to the empty string for expiration time, and again, tab-separated):



    my-example.com  FALSE   /   FALSE       _rails-root_session test


    then it outputs 1 (successfully read the cookie).



    What needs to be done to my python code to read the original cookie line? And preferably to be able to save it in the same format (with HttpOnly flag and with 0 instead of empty string for never-expiring cookie)?



    Thanks.










    share|improve this question


























      up vote
      3
      down vote

      favorite
      1









      up vote
      3
      down vote

      favorite
      1






      1





      I have the following cookie saved by curl (in test.txt, tab-separated, this editor doesn't preserve tabs):



      # Netscape HTTP Cookie File
      # http://curlm.haxx.se/rfc/cookie_spec.html
      # This file was generated by libcurl! Edit at your own risk.

      #HttpOnly_my-example.com FALSE / FALSE 0 _rails-root_session test


      I'm trying to read it with the following code:



      import sys

      if sys.version_info < (3,):
      from cookielib import Cookie, MozillaCookieJar
      else:
      from http.cookiejar import Cookie, MozillaCookieJar

      def load_cookies_from_mozilla(filename):
      ns_cookiejar = MozillaCookieJar()
      ns_cookiejar.load(filename, ignore_discard=True)
      return ns_cookiejar

      cookies = load_cookies_from_mozilla("test.txt")
      print (len(cookies))


      It outputs 0 (unable to read the cookie).
      If I manually modify my cookie to the following line (remove HttpOnly flag and changing 0 to the empty string for expiration time, and again, tab-separated):



      my-example.com  FALSE   /   FALSE       _rails-root_session test


      then it outputs 1 (successfully read the cookie).



      What needs to be done to my python code to read the original cookie line? And preferably to be able to save it in the same format (with HttpOnly flag and with 0 instead of empty string for never-expiring cookie)?



      Thanks.










      share|improve this question















      I have the following cookie saved by curl (in test.txt, tab-separated, this editor doesn't preserve tabs):



      # Netscape HTTP Cookie File
      # http://curlm.haxx.se/rfc/cookie_spec.html
      # This file was generated by libcurl! Edit at your own risk.

      #HttpOnly_my-example.com FALSE / FALSE 0 _rails-root_session test


      I'm trying to read it with the following code:



      import sys

      if sys.version_info < (3,):
      from cookielib import Cookie, MozillaCookieJar
      else:
      from http.cookiejar import Cookie, MozillaCookieJar

      def load_cookies_from_mozilla(filename):
      ns_cookiejar = MozillaCookieJar()
      ns_cookiejar.load(filename, ignore_discard=True)
      return ns_cookiejar

      cookies = load_cookies_from_mozilla("test.txt")
      print (len(cookies))


      It outputs 0 (unable to read the cookie).
      If I manually modify my cookie to the following line (remove HttpOnly flag and changing 0 to the empty string for expiration time, and again, tab-separated):



      my-example.com  FALSE   /   FALSE       _rails-root_session test


      then it outputs 1 (successfully read the cookie).



      What needs to be done to my python code to read the original cookie line? And preferably to be able to save it in the same format (with HttpOnly flag and with 0 instead of empty string for never-expiring cookie)?



      Thanks.







      python curl cookies






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited May 24 '16 at 17:52

























      asked May 23 '16 at 20:39









      some

      1266




      1266
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          I tested your code and modified it, it works.
          First in the cookie file you have to put off the '#' before your cookie, I think it will comment the data after it.
          Second the 0 in the cookie means the expire time, 0 means expire now, so you can change the 0 to empty string or latter time, but i suggest you use the argument ignore_expire=True, the official means:




          ignore_discard: save even cookies set to be discarded.



          ignore_expires: save even cookies that have expiredThe file is overwritten if it already exists




          and the result code is :



          import sys
          if sys.version_info < (3,):
          from cookielib import Cookie, MozillaCookieJar
          else:
          from http.cookiejar import Cookie, MozillaCookieJar

          def load_cookies_from_mozilla(filename):
          ns_cookiejar = MozillaCookieJar()
          ns_cookiejar.load(filename, ignore_discard=True, ignore_expires=True)
          return ns_cookiejar

          cookies = load_cookies_from_mozilla("test.txt")
          print (len(cookies))


          and you can see the link to find more detail:
          Using cookies.txt file with Python Requests






          share|improve this answer























          • Yes, igonore_expires=True allows to read cookies with 0 ttl. The question with #HttpOnly_ still remains (it's not a comment) - is there a way to say somehow to python that I do want to read cookies with HttpOnly flag set? Of course, I can (and I did) manually open the cookiefile and modify it (erase #HttpOnly_) but it would be better to use a legal way (if it exists).
            – some
            Sep 3 '16 at 13:34












          • If you don't want to read some cookie you can just comment it with '#' and if you said #HttpOnly_ is not a comment, then i don't know how to salve your problem, i don't know how to use a legal way to avoid read cookie that you set a flag
            – Fisher
            Sep 3 '16 at 15:39


















          up vote
          0
          down vote













          This appears to be an open bug: https://bugs.python.org/issue2190.






          share|improve this answer





















            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37400140%2freading-cookies-file-created-by-curl%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            I tested your code and modified it, it works.
            First in the cookie file you have to put off the '#' before your cookie, I think it will comment the data after it.
            Second the 0 in the cookie means the expire time, 0 means expire now, so you can change the 0 to empty string or latter time, but i suggest you use the argument ignore_expire=True, the official means:




            ignore_discard: save even cookies set to be discarded.



            ignore_expires: save even cookies that have expiredThe file is overwritten if it already exists




            and the result code is :



            import sys
            if sys.version_info < (3,):
            from cookielib import Cookie, MozillaCookieJar
            else:
            from http.cookiejar import Cookie, MozillaCookieJar

            def load_cookies_from_mozilla(filename):
            ns_cookiejar = MozillaCookieJar()
            ns_cookiejar.load(filename, ignore_discard=True, ignore_expires=True)
            return ns_cookiejar

            cookies = load_cookies_from_mozilla("test.txt")
            print (len(cookies))


            and you can see the link to find more detail:
            Using cookies.txt file with Python Requests






            share|improve this answer























            • Yes, igonore_expires=True allows to read cookies with 0 ttl. The question with #HttpOnly_ still remains (it's not a comment) - is there a way to say somehow to python that I do want to read cookies with HttpOnly flag set? Of course, I can (and I did) manually open the cookiefile and modify it (erase #HttpOnly_) but it would be better to use a legal way (if it exists).
              – some
              Sep 3 '16 at 13:34












            • If you don't want to read some cookie you can just comment it with '#' and if you said #HttpOnly_ is not a comment, then i don't know how to salve your problem, i don't know how to use a legal way to avoid read cookie that you set a flag
              – Fisher
              Sep 3 '16 at 15:39















            up vote
            1
            down vote













            I tested your code and modified it, it works.
            First in the cookie file you have to put off the '#' before your cookie, I think it will comment the data after it.
            Second the 0 in the cookie means the expire time, 0 means expire now, so you can change the 0 to empty string or latter time, but i suggest you use the argument ignore_expire=True, the official means:




            ignore_discard: save even cookies set to be discarded.



            ignore_expires: save even cookies that have expiredThe file is overwritten if it already exists




            and the result code is :



            import sys
            if sys.version_info < (3,):
            from cookielib import Cookie, MozillaCookieJar
            else:
            from http.cookiejar import Cookie, MozillaCookieJar

            def load_cookies_from_mozilla(filename):
            ns_cookiejar = MozillaCookieJar()
            ns_cookiejar.load(filename, ignore_discard=True, ignore_expires=True)
            return ns_cookiejar

            cookies = load_cookies_from_mozilla("test.txt")
            print (len(cookies))


            and you can see the link to find more detail:
            Using cookies.txt file with Python Requests






            share|improve this answer























            • Yes, igonore_expires=True allows to read cookies with 0 ttl. The question with #HttpOnly_ still remains (it's not a comment) - is there a way to say somehow to python that I do want to read cookies with HttpOnly flag set? Of course, I can (and I did) manually open the cookiefile and modify it (erase #HttpOnly_) but it would be better to use a legal way (if it exists).
              – some
              Sep 3 '16 at 13:34












            • If you don't want to read some cookie you can just comment it with '#' and if you said #HttpOnly_ is not a comment, then i don't know how to salve your problem, i don't know how to use a legal way to avoid read cookie that you set a flag
              – Fisher
              Sep 3 '16 at 15:39













            up vote
            1
            down vote










            up vote
            1
            down vote









            I tested your code and modified it, it works.
            First in the cookie file you have to put off the '#' before your cookie, I think it will comment the data after it.
            Second the 0 in the cookie means the expire time, 0 means expire now, so you can change the 0 to empty string or latter time, but i suggest you use the argument ignore_expire=True, the official means:




            ignore_discard: save even cookies set to be discarded.



            ignore_expires: save even cookies that have expiredThe file is overwritten if it already exists




            and the result code is :



            import sys
            if sys.version_info < (3,):
            from cookielib import Cookie, MozillaCookieJar
            else:
            from http.cookiejar import Cookie, MozillaCookieJar

            def load_cookies_from_mozilla(filename):
            ns_cookiejar = MozillaCookieJar()
            ns_cookiejar.load(filename, ignore_discard=True, ignore_expires=True)
            return ns_cookiejar

            cookies = load_cookies_from_mozilla("test.txt")
            print (len(cookies))


            and you can see the link to find more detail:
            Using cookies.txt file with Python Requests






            share|improve this answer














            I tested your code and modified it, it works.
            First in the cookie file you have to put off the '#' before your cookie, I think it will comment the data after it.
            Second the 0 in the cookie means the expire time, 0 means expire now, so you can change the 0 to empty string or latter time, but i suggest you use the argument ignore_expire=True, the official means:




            ignore_discard: save even cookies set to be discarded.



            ignore_expires: save even cookies that have expiredThe file is overwritten if it already exists




            and the result code is :



            import sys
            if sys.version_info < (3,):
            from cookielib import Cookie, MozillaCookieJar
            else:
            from http.cookiejar import Cookie, MozillaCookieJar

            def load_cookies_from_mozilla(filename):
            ns_cookiejar = MozillaCookieJar()
            ns_cookiejar.load(filename, ignore_discard=True, ignore_expires=True)
            return ns_cookiejar

            cookies = load_cookies_from_mozilla("test.txt")
            print (len(cookies))


            and you can see the link to find more detail:
            Using cookies.txt file with Python Requests







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited May 23 '17 at 12:32









            Community

            11




            11










            answered Sep 2 '16 at 2:20









            Fisher

            112




            112












            • Yes, igonore_expires=True allows to read cookies with 0 ttl. The question with #HttpOnly_ still remains (it's not a comment) - is there a way to say somehow to python that I do want to read cookies with HttpOnly flag set? Of course, I can (and I did) manually open the cookiefile and modify it (erase #HttpOnly_) but it would be better to use a legal way (if it exists).
              – some
              Sep 3 '16 at 13:34












            • If you don't want to read some cookie you can just comment it with '#' and if you said #HttpOnly_ is not a comment, then i don't know how to salve your problem, i don't know how to use a legal way to avoid read cookie that you set a flag
              – Fisher
              Sep 3 '16 at 15:39


















            • Yes, igonore_expires=True allows to read cookies with 0 ttl. The question with #HttpOnly_ still remains (it's not a comment) - is there a way to say somehow to python that I do want to read cookies with HttpOnly flag set? Of course, I can (and I did) manually open the cookiefile and modify it (erase #HttpOnly_) but it would be better to use a legal way (if it exists).
              – some
              Sep 3 '16 at 13:34












            • If you don't want to read some cookie you can just comment it with '#' and if you said #HttpOnly_ is not a comment, then i don't know how to salve your problem, i don't know how to use a legal way to avoid read cookie that you set a flag
              – Fisher
              Sep 3 '16 at 15:39
















            Yes, igonore_expires=True allows to read cookies with 0 ttl. The question with #HttpOnly_ still remains (it's not a comment) - is there a way to say somehow to python that I do want to read cookies with HttpOnly flag set? Of course, I can (and I did) manually open the cookiefile and modify it (erase #HttpOnly_) but it would be better to use a legal way (if it exists).
            – some
            Sep 3 '16 at 13:34






            Yes, igonore_expires=True allows to read cookies with 0 ttl. The question with #HttpOnly_ still remains (it's not a comment) - is there a way to say somehow to python that I do want to read cookies with HttpOnly flag set? Of course, I can (and I did) manually open the cookiefile and modify it (erase #HttpOnly_) but it would be better to use a legal way (if it exists).
            – some
            Sep 3 '16 at 13:34














            If you don't want to read some cookie you can just comment it with '#' and if you said #HttpOnly_ is not a comment, then i don't know how to salve your problem, i don't know how to use a legal way to avoid read cookie that you set a flag
            – Fisher
            Sep 3 '16 at 15:39




            If you don't want to read some cookie you can just comment it with '#' and if you said #HttpOnly_ is not a comment, then i don't know how to salve your problem, i don't know how to use a legal way to avoid read cookie that you set a flag
            – Fisher
            Sep 3 '16 at 15:39












            up vote
            0
            down vote













            This appears to be an open bug: https://bugs.python.org/issue2190.






            share|improve this answer

























              up vote
              0
              down vote













              This appears to be an open bug: https://bugs.python.org/issue2190.






              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                This appears to be an open bug: https://bugs.python.org/issue2190.






                share|improve this answer












                This appears to be an open bug: https://bugs.python.org/issue2190.







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 19 at 23:46









                Mitch

                1,108918




                1,108918






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f37400140%2freading-cookies-file-created-by-curl%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Costa Masnaga

                    Fotorealismo

                    Sidney Franklin