Created
May 13, 2019 21:20
-
-
Save markcam1/1c8253f52cbb032ef5f66326e38bf7a6 to your computer and use it in GitHub Desktop.
HTTP Requests for Python with username/password example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import json | |
import logging | |
import getpass | |
import os | |
#logging config: | |
logging.basicConfig(level=logging.DEBUG, | |
format=' %(asctime)s - %(levelname)s - %(message)s') | |
logging.debug('Start of REMOTE DATA.................') | |
class WebsiteClass: | |
def __init__(self, url, file_name): | |
self.url = url | |
self.username = '[email protected]' | |
self.password = WebsiteClass.ask_password(self) | |
self.basic_auth = (self.username, self.password) | |
self.file_name = file_name | |
self.script_dir = os.path.dirname(os.path.abspath(__file__)) | |
self.customer_data_dir = os.path.join(self.script_dir, 'data') | |
def ask_password(self): | |
try: | |
PASSWORD_ENTERED = getpass.getpass() | |
return PASSWORD_ENTERED | |
except Exception as error: | |
print('ERROR', error) | |
def get_this_site(self): | |
try: | |
response = requests.get(self.url, auth=self.basic_auth) | |
except requests.exceptions.RequestException as e: | |
print('ERROR', e) | |
if response.status_code == 200: | |
res_text = response.text | |
save_file = open('this.txt', 'w') | |
save_file.write(res_text) | |
save_file.close() | |
def get_that_website(self): | |
try: | |
os.makedirs(self.customer_data_dir) | |
except FileExistsError: | |
pass | |
try: | |
response = requests.get(self.url, auth=self.basic_auth) | |
except requests.exceptions.RequestException as e: | |
print('ERROR', e) | |
if response.status_code == 200: | |
handle_zip_data = open('whateva.txt', 'wb') | |
for chunk in response.iter_content(10000): | |
handle_zip_data.write(chunk) | |
handle_zip_data.close() | |
return True | |
def main(): | |
new_instance = WebsiteClass('http://example.com/', 'target.json') | |
if __name__=='__main__': | |
main() | |
logging.debug('End program_REMOTEDATA') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment