How to keep Http client for digest authentication alive while using serverless










0















I am developing a Python application which sends data to a web server via their REST API. The server uses digest authentication for the API calls. The theory behind digest authentication is to authenticate client in two steps. The initial hand shake uses a token, client receives WWW-Authenticate header from server and a new nonce and nonce count gets created. Then the second call is made with the actual API request with real data.



The original design is to keep HTTP client alive once the initial connection is established to have the client increment the nonce count with every request during the lifetime of the nonce. This will avoid the overhead of creating a thread and re-instatiating an HTTP client every time we need to send data to the server. The application is running as a service on a EC2 linux instance in AWS. So it is totally fine to keep the HTTP client alive.



import requests

sess = requests.Session()
sess.auth = HTTPDigestAuth(USERNAME, TOKEN)
sess.verify = False
sess.mount('https://', HTTPAdapter(max_retries=HTTP_CLIENT_RETRY_MAX))


However for next phase we are thinking going serverless, Lambda function to be specific. It scales much better and we don't need to maintain the EC2 server. The problem is every time Lambda function runs to send data to server, it has no memory of the previous API call. That means it will re-instantiating an HTTP client every time which is not efficient. I wonder if there is a way to keep a HTTP client alive using Lambda function.










share|improve this question




























    0















    I am developing a Python application which sends data to a web server via their REST API. The server uses digest authentication for the API calls. The theory behind digest authentication is to authenticate client in two steps. The initial hand shake uses a token, client receives WWW-Authenticate header from server and a new nonce and nonce count gets created. Then the second call is made with the actual API request with real data.



    The original design is to keep HTTP client alive once the initial connection is established to have the client increment the nonce count with every request during the lifetime of the nonce. This will avoid the overhead of creating a thread and re-instatiating an HTTP client every time we need to send data to the server. The application is running as a service on a EC2 linux instance in AWS. So it is totally fine to keep the HTTP client alive.



    import requests

    sess = requests.Session()
    sess.auth = HTTPDigestAuth(USERNAME, TOKEN)
    sess.verify = False
    sess.mount('https://', HTTPAdapter(max_retries=HTTP_CLIENT_RETRY_MAX))


    However for next phase we are thinking going serverless, Lambda function to be specific. It scales much better and we don't need to maintain the EC2 server. The problem is every time Lambda function runs to send data to server, it has no memory of the previous API call. That means it will re-instantiating an HTTP client every time which is not efficient. I wonder if there is a way to keep a HTTP client alive using Lambda function.










    share|improve this question


























      0












      0








      0








      I am developing a Python application which sends data to a web server via their REST API. The server uses digest authentication for the API calls. The theory behind digest authentication is to authenticate client in two steps. The initial hand shake uses a token, client receives WWW-Authenticate header from server and a new nonce and nonce count gets created. Then the second call is made with the actual API request with real data.



      The original design is to keep HTTP client alive once the initial connection is established to have the client increment the nonce count with every request during the lifetime of the nonce. This will avoid the overhead of creating a thread and re-instatiating an HTTP client every time we need to send data to the server. The application is running as a service on a EC2 linux instance in AWS. So it is totally fine to keep the HTTP client alive.



      import requests

      sess = requests.Session()
      sess.auth = HTTPDigestAuth(USERNAME, TOKEN)
      sess.verify = False
      sess.mount('https://', HTTPAdapter(max_retries=HTTP_CLIENT_RETRY_MAX))


      However for next phase we are thinking going serverless, Lambda function to be specific. It scales much better and we don't need to maintain the EC2 server. The problem is every time Lambda function runs to send data to server, it has no memory of the previous API call. That means it will re-instantiating an HTTP client every time which is not efficient. I wonder if there is a way to keep a HTTP client alive using Lambda function.










      share|improve this question
















      I am developing a Python application which sends data to a web server via their REST API. The server uses digest authentication for the API calls. The theory behind digest authentication is to authenticate client in two steps. The initial hand shake uses a token, client receives WWW-Authenticate header from server and a new nonce and nonce count gets created. Then the second call is made with the actual API request with real data.



      The original design is to keep HTTP client alive once the initial connection is established to have the client increment the nonce count with every request during the lifetime of the nonce. This will avoid the overhead of creating a thread and re-instatiating an HTTP client every time we need to send data to the server. The application is running as a service on a EC2 linux instance in AWS. So it is totally fine to keep the HTTP client alive.



      import requests

      sess = requests.Session()
      sess.auth = HTTPDigestAuth(USERNAME, TOKEN)
      sess.verify = False
      sess.mount('https://', HTTPAdapter(max_retries=HTTP_CLIENT_RETRY_MAX))


      However for next phase we are thinking going serverless, Lambda function to be specific. It scales much better and we don't need to maintain the EC2 server. The problem is every time Lambda function runs to send data to server, it has no memory of the previous API call. That means it will re-instantiating an HTTP client every time which is not efficient. I wonder if there is a way to keep a HTTP client alive using Lambda function.







      python amazon-web-services aws-lambda serverless digest-authentication






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 15 '18 at 5:35









      John Rotenstein

      72.7k782127




      72.7k782127










      asked Nov 15 '18 at 3:56









      dddddd

      1,18022354




      1,18022354






















          0






          active

          oldest

          votes











          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',
          autoActivateHeartbeat: false,
          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%2f53312218%2fhow-to-keep-http-client-for-digest-authentication-alive-while-using-serverless%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          0






          active

          oldest

          votes








          0






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes















          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53312218%2fhow-to-keep-http-client-for-digest-authentication-alive-while-using-serverless%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

          Top Tejano songwriter Luis Silva dead of heart attack at 64

          政党

          天津地下鉄3号線