HTTP Error 403: Forbidden while downloading file using urllib












2














I have this line of code: urllib.request.urlretrieve('http://lolupdater.com/downloads/LPB.exe', 'LPBtest.exe'), but when I run it, it throws an error urllib.error.HTTPError: HTTP Error 403: Forbidden.










share|improve this question





























    2














    I have this line of code: urllib.request.urlretrieve('http://lolupdater.com/downloads/LPB.exe', 'LPBtest.exe'), but when I run it, it throws an error urllib.error.HTTPError: HTTP Error 403: Forbidden.










    share|improve this question



























      2












      2








      2







      I have this line of code: urllib.request.urlretrieve('http://lolupdater.com/downloads/LPB.exe', 'LPBtest.exe'), but when I run it, it throws an error urllib.error.HTTPError: HTTP Error 403: Forbidden.










      share|improve this question















      I have this line of code: urllib.request.urlretrieve('http://lolupdater.com/downloads/LPB.exe', 'LPBtest.exe'), but when I run it, it throws an error urllib.error.HTTPError: HTTP Error 403: Forbidden.







      python-3.x file download urllib






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 11 at 22:11









      Trilarion

      6,51753876




      6,51753876










      asked Jul 27 '17 at 18:09









      Jakub Bláha

      455225




      455225
























          1 Answer
          1






          active

          oldest

          votes


















          2














          That looks to be an actual HTTP 403: Forbidden error. Python urllib throws the exception when it encounters an HTTP status code (documented here). 403 in general means: "The server understood the request, but is refusing to fulfill it." You will need to add HTTP headers to identify yourself and avoid the 403 error, documentation on Python urllib headers. Here is an example using urlopen:



          import urllib.request
          req = urllib.request.Request('http://lolupdater.com/downloads/LPB.exe', headers={'User-Agent': 'Mozilla/5.0'})
          response = urllib.request.urlopen(req)


          With Python 3 urllib.urlretrieve() is considered legacy. I would recommend Python Requests for this, here is a working example:



          import requests

          url = 'http://lolupdater.com/downloads/LPB.exe'
          r = requests.get(url)
          with open('LPBtest.exe', 'wb') as outfile:
          outfile.write(r.content)





          share|improve this answer























          • But how to save the data than if I have an executable?
            – Jakub Bláha
            Jul 27 '17 at 19:05










          • @JakubBláha I updated my answer. It looks like urllib.urlretrieve() doesn't allow you to set headers. I would recommend using Python Requests, its definitely the more accepted way to do what you are trying to do, hope it helps.
            – andrew
            Jul 27 '17 at 19:57













          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%2f45358126%2fhttp-error-403-forbidden-while-downloading-file-using-urllib%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          That looks to be an actual HTTP 403: Forbidden error. Python urllib throws the exception when it encounters an HTTP status code (documented here). 403 in general means: "The server understood the request, but is refusing to fulfill it." You will need to add HTTP headers to identify yourself and avoid the 403 error, documentation on Python urllib headers. Here is an example using urlopen:



          import urllib.request
          req = urllib.request.Request('http://lolupdater.com/downloads/LPB.exe', headers={'User-Agent': 'Mozilla/5.0'})
          response = urllib.request.urlopen(req)


          With Python 3 urllib.urlretrieve() is considered legacy. I would recommend Python Requests for this, here is a working example:



          import requests

          url = 'http://lolupdater.com/downloads/LPB.exe'
          r = requests.get(url)
          with open('LPBtest.exe', 'wb') as outfile:
          outfile.write(r.content)





          share|improve this answer























          • But how to save the data than if I have an executable?
            – Jakub Bláha
            Jul 27 '17 at 19:05










          • @JakubBláha I updated my answer. It looks like urllib.urlretrieve() doesn't allow you to set headers. I would recommend using Python Requests, its definitely the more accepted way to do what you are trying to do, hope it helps.
            – andrew
            Jul 27 '17 at 19:57


















          2














          That looks to be an actual HTTP 403: Forbidden error. Python urllib throws the exception when it encounters an HTTP status code (documented here). 403 in general means: "The server understood the request, but is refusing to fulfill it." You will need to add HTTP headers to identify yourself and avoid the 403 error, documentation on Python urllib headers. Here is an example using urlopen:



          import urllib.request
          req = urllib.request.Request('http://lolupdater.com/downloads/LPB.exe', headers={'User-Agent': 'Mozilla/5.0'})
          response = urllib.request.urlopen(req)


          With Python 3 urllib.urlretrieve() is considered legacy. I would recommend Python Requests for this, here is a working example:



          import requests

          url = 'http://lolupdater.com/downloads/LPB.exe'
          r = requests.get(url)
          with open('LPBtest.exe', 'wb') as outfile:
          outfile.write(r.content)





          share|improve this answer























          • But how to save the data than if I have an executable?
            – Jakub Bláha
            Jul 27 '17 at 19:05










          • @JakubBláha I updated my answer. It looks like urllib.urlretrieve() doesn't allow you to set headers. I would recommend using Python Requests, its definitely the more accepted way to do what you are trying to do, hope it helps.
            – andrew
            Jul 27 '17 at 19:57
















          2












          2








          2






          That looks to be an actual HTTP 403: Forbidden error. Python urllib throws the exception when it encounters an HTTP status code (documented here). 403 in general means: "The server understood the request, but is refusing to fulfill it." You will need to add HTTP headers to identify yourself and avoid the 403 error, documentation on Python urllib headers. Here is an example using urlopen:



          import urllib.request
          req = urllib.request.Request('http://lolupdater.com/downloads/LPB.exe', headers={'User-Agent': 'Mozilla/5.0'})
          response = urllib.request.urlopen(req)


          With Python 3 urllib.urlretrieve() is considered legacy. I would recommend Python Requests for this, here is a working example:



          import requests

          url = 'http://lolupdater.com/downloads/LPB.exe'
          r = requests.get(url)
          with open('LPBtest.exe', 'wb') as outfile:
          outfile.write(r.content)





          share|improve this answer














          That looks to be an actual HTTP 403: Forbidden error. Python urllib throws the exception when it encounters an HTTP status code (documented here). 403 in general means: "The server understood the request, but is refusing to fulfill it." You will need to add HTTP headers to identify yourself and avoid the 403 error, documentation on Python urllib headers. Here is an example using urlopen:



          import urllib.request
          req = urllib.request.Request('http://lolupdater.com/downloads/LPB.exe', headers={'User-Agent': 'Mozilla/5.0'})
          response = urllib.request.urlopen(req)


          With Python 3 urllib.urlretrieve() is considered legacy. I would recommend Python Requests for this, here is a working example:



          import requests

          url = 'http://lolupdater.com/downloads/LPB.exe'
          r = requests.get(url)
          with open('LPBtest.exe', 'wb') as outfile:
          outfile.write(r.content)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Jul 27 '17 at 19:54

























          answered Jul 27 '17 at 18:49









          andrew

          1,69011323




          1,69011323












          • But how to save the data than if I have an executable?
            – Jakub Bláha
            Jul 27 '17 at 19:05










          • @JakubBláha I updated my answer. It looks like urllib.urlretrieve() doesn't allow you to set headers. I would recommend using Python Requests, its definitely the more accepted way to do what you are trying to do, hope it helps.
            – andrew
            Jul 27 '17 at 19:57




















          • But how to save the data than if I have an executable?
            – Jakub Bláha
            Jul 27 '17 at 19:05










          • @JakubBláha I updated my answer. It looks like urllib.urlretrieve() doesn't allow you to set headers. I would recommend using Python Requests, its definitely the more accepted way to do what you are trying to do, hope it helps.
            – andrew
            Jul 27 '17 at 19:57


















          But how to save the data than if I have an executable?
          – Jakub Bláha
          Jul 27 '17 at 19:05




          But how to save the data than if I have an executable?
          – Jakub Bláha
          Jul 27 '17 at 19:05












          @JakubBláha I updated my answer. It looks like urllib.urlretrieve() doesn't allow you to set headers. I would recommend using Python Requests, its definitely the more accepted way to do what you are trying to do, hope it helps.
          – andrew
          Jul 27 '17 at 19:57






          @JakubBláha I updated my answer. It looks like urllib.urlretrieve() doesn't allow you to set headers. I would recommend using Python Requests, its definitely the more accepted way to do what you are trying to do, hope it helps.
          – andrew
          Jul 27 '17 at 19:57




















          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%2f45358126%2fhttp-error-403-forbidden-while-downloading-file-using-urllib%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

          Full-time equivalent

          さくらももこ

          13 indicted, 8 arrested in Calif. drug cartel investigation