Python: How to get rid of timeout errors when you're using external network APIs?











up vote
1
down vote

favorite












When I am using postman tool everything is working fine but when I am trying to get access token using Python http client / requests. both times ending up following Errors.



When I use http.client



TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond


or when I use requests , session thing



ConnectionError: HTTPSConnectionPool(host='identity.trimble.com', port=443): Max retries exceeded with url: /token (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x00000000088F49E8>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',))


this is my code



import http.client, urllib.request, urllib.parse, urllib.error, base64
url = 'https://someurl'

client_id = 'some key' #Consumer Key
client_secret = 'secret' #Consumer Secret

Auth = base64.b64encode("{id}:{secrete}".format(**{'id' : client_id,
'secrete': client_secret}).encode('utf-8'))
header = {
'authorization': 'Basic %s' % Auth.decode('utf-8'),
'content-type' : 'application/x-www-form-urlencoded',
'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
}
body = urllib.parse.urlencode({
'grant_type' : 'password',
'username' : 'usr@gmail.com',
'password' : 'somepwd',
'scope' : 'scope'
})
conn = http.client.HTTPSConnection('identity.trimble.com')

conn.request("POST", "/token", body=body, headers=header)
response = conn.getresponse()
print(response.code)
if response.code == 200:
data = json.loads(response.read())
print(data.get('access_token', None))
#access_token['ttl'] = datetime.now() + timedelta(seconds=data.get('expires_in', 10))
conn.close()


I am in my company VPN does this have any effect? I am wondering because it's working fine with Postman tool.



Thanks










share|improve this question




























    up vote
    1
    down vote

    favorite












    When I am using postman tool everything is working fine but when I am trying to get access token using Python http client / requests. both times ending up following Errors.



    When I use http.client



    TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond


    or when I use requests , session thing



    ConnectionError: HTTPSConnectionPool(host='identity.trimble.com', port=443): Max retries exceeded with url: /token (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x00000000088F49E8>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',))


    this is my code



    import http.client, urllib.request, urllib.parse, urllib.error, base64
    url = 'https://someurl'

    client_id = 'some key' #Consumer Key
    client_secret = 'secret' #Consumer Secret

    Auth = base64.b64encode("{id}:{secrete}".format(**{'id' : client_id,
    'secrete': client_secret}).encode('utf-8'))
    header = {
    'authorization': 'Basic %s' % Auth.decode('utf-8'),
    'content-type' : 'application/x-www-form-urlencoded',
    'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
    }
    body = urllib.parse.urlencode({
    'grant_type' : 'password',
    'username' : 'usr@gmail.com',
    'password' : 'somepwd',
    'scope' : 'scope'
    })
    conn = http.client.HTTPSConnection('identity.trimble.com')

    conn.request("POST", "/token", body=body, headers=header)
    response = conn.getresponse()
    print(response.code)
    if response.code == 200:
    data = json.loads(response.read())
    print(data.get('access_token', None))
    #access_token['ttl'] = datetime.now() + timedelta(seconds=data.get('expires_in', 10))
    conn.close()


    I am in my company VPN does this have any effect? I am wondering because it's working fine with Postman tool.



    Thanks










    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      When I am using postman tool everything is working fine but when I am trying to get access token using Python http client / requests. both times ending up following Errors.



      When I use http.client



      TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond


      or when I use requests , session thing



      ConnectionError: HTTPSConnectionPool(host='identity.trimble.com', port=443): Max retries exceeded with url: /token (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x00000000088F49E8>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',))


      this is my code



      import http.client, urllib.request, urllib.parse, urllib.error, base64
      url = 'https://someurl'

      client_id = 'some key' #Consumer Key
      client_secret = 'secret' #Consumer Secret

      Auth = base64.b64encode("{id}:{secrete}".format(**{'id' : client_id,
      'secrete': client_secret}).encode('utf-8'))
      header = {
      'authorization': 'Basic %s' % Auth.decode('utf-8'),
      'content-type' : 'application/x-www-form-urlencoded',
      'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
      }
      body = urllib.parse.urlencode({
      'grant_type' : 'password',
      'username' : 'usr@gmail.com',
      'password' : 'somepwd',
      'scope' : 'scope'
      })
      conn = http.client.HTTPSConnection('identity.trimble.com')

      conn.request("POST", "/token", body=body, headers=header)
      response = conn.getresponse()
      print(response.code)
      if response.code == 200:
      data = json.loads(response.read())
      print(data.get('access_token', None))
      #access_token['ttl'] = datetime.now() + timedelta(seconds=data.get('expires_in', 10))
      conn.close()


      I am in my company VPN does this have any effect? I am wondering because it's working fine with Postman tool.



      Thanks










      share|improve this question















      When I am using postman tool everything is working fine but when I am trying to get access token using Python http client / requests. both times ending up following Errors.



      When I use http.client



      TimeoutError: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond


      or when I use requests , session thing



      ConnectionError: HTTPSConnectionPool(host='identity.trimble.com', port=443): Max retries exceeded with url: /token (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x00000000088F49E8>: Failed to establish a new connection: [WinError 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because connected host has failed to respond',))


      this is my code



      import http.client, urllib.request, urllib.parse, urllib.error, base64
      url = 'https://someurl'

      client_id = 'some key' #Consumer Key
      client_secret = 'secret' #Consumer Secret

      Auth = base64.b64encode("{id}:{secrete}".format(**{'id' : client_id,
      'secrete': client_secret}).encode('utf-8'))
      header = {
      'authorization': 'Basic %s' % Auth.decode('utf-8'),
      'content-type' : 'application/x-www-form-urlencoded',
      'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'
      }
      body = urllib.parse.urlencode({
      'grant_type' : 'password',
      'username' : 'usr@gmail.com',
      'password' : 'somepwd',
      'scope' : 'scope'
      })
      conn = http.client.HTTPSConnection('identity.trimble.com')

      conn.request("POST", "/token", body=body, headers=header)
      response = conn.getresponse()
      print(response.code)
      if response.code == 200:
      data = json.loads(response.read())
      print(data.get('access_token', None))
      #access_token['ttl'] = datetime.now() + timedelta(seconds=data.get('expires_in', 10))
      conn.close()


      I am in my company VPN does this have any effect? I am wondering because it's working fine with Postman tool.



      Thanks







      python python-3.x python-requests






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 7 at 7:51

























      asked Nov 7 at 7:43









      CSMaverick

      1,2081328




      1,2081328





























          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',
          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%2f53185280%2fpython-how-to-get-rid-of-timeout-errors-when-youre-using-external-network-apis%23new-answer', 'question_page');
          }
          );

          Post as a guest





































          active

          oldest

          votes













          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes
















           

          draft saved


          draft discarded



















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53185280%2fpython-how-to-get-rid-of-timeout-errors-when-youre-using-external-network-apis%23new-answer', 'question_page');
          }
          );

          Post as a guest




















































































          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()