-
-
Save ChiranjeeviAdi/3d37c768d8202acb4bff816874ef9cc4 to your computer and use it in GitHub Desktop.
SFTP - Transfer file to server using fastapi
This file contains hidden or 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
| HOST= | |
| USERNAME= | |
| PASSWORD= | |
| WRITE_PATH=folder_writable_path/ | |
| API_ENDPOINT= |
This file contains hidden or 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
| from typing import Optional | |
| from fastapi import FastAPI,HTTPException | |
| import pysftp | |
| import paramiko | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| HOST = os.getenv('HOST') | |
| USERNAME = os.getenv('USERNAME') | |
| PASSWORD = os.getenv('PASSWORD') | |
| path = os.getenv('WRITE_PATH') #folder path | |
| class My_Connection(pysftp.Connection): | |
| def __init__(self, *args, **kwargs): | |
| try: | |
| if kwargs.get('cnopts') is None: | |
| kwargs['cnopts'] = pysftp.CnOpts() | |
| except pysftp.HostKeysException as e: | |
| self._init_error = True | |
| raise paramiko.ssh_exception.SSHException(str(e)) | |
| else: | |
| self._init_error = False | |
| self._sftp_live = False | |
| self._transport = None | |
| super().__init__(*args, **kwargs) | |
| def __del__(self): | |
| if not self._init_error: | |
| self.close() | |
| #UploadFile function to move local generated file to sftp server | |
| def uploadFile(filePath): | |
| statusFileUpload=200 | |
| try: | |
| cnopts = pysftp.CnOpts() | |
| cnopts.hostkeys = None | |
| sftp = My_Connection(HOST, username=USERNAME, password=PASSWORD,cnopts=cnopts) | |
| # data = sftp.listdir() | |
| # print(data) | |
| sftp.chdir(path) | |
| sftp.put(filePath) | |
| sftp.close() | |
| statusFileUpload = 200 | |
| except Exception as e: | |
| statusFileUpload = str(e) | |
| finally: | |
| statusFileUpload | |
| return statusFileUpload | |
This file contains hidden or 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
| from classes.syncFile import syncLead | |
| from typing import Optional | |
| from fastapi import FastAPI,HTTPException | |
| import pysftp | |
| import requests | |
| import json | |
| import requests as req | |
| import util | |
| import ftp | |
| import uvicorn | |
| import datetime | |
| import pandas as pd | |
| @app.post('/syncFile') | |
| def syncFile(): | |
| #getting data | |
| data = util.getData() | |
| if data!='': | |
| #write data to today's temp file | |
| filename = util.getFileName('json') | |
| filePath = './data/'+filename | |
| f = open(filePath, "w") | |
| f.write(json.dumps(data['emps'])) | |
| f.close() | |
| df = pd.read_json(filePath) | |
| #Convert json to csv using pandas library | |
| filenameCsv = util.getFileName('csv') | |
| filePathCsv = './data/'+filenameCsv | |
| f = open(filePathCsv, "w") | |
| f.close() | |
| df.to_csv(filePathCsv, index = None) | |
| statusFileUpload = ftp.uploadFile(filePathCsv) | |
| if statusFileUpload == 200: | |
| return {"message":"File moved successfully"} | |
| else | |
| raise HTTPException(status_code=403, detail="Error: File not moved") | |
| else: | |
| raise HTTPException(status_code=403, detail="Error: Failed to fetch lead data") |
This file contains hidden or 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
| aniso8601==7.0.0 | |
| asgiref==3.4.1 | |
| async-exit-stack==1.0.1 | |
| async-generator==1.10 | |
| asyncpg==0.24.0 | |
| attrs==21.2.0 | |
| bcrypt==3.2.0 | |
| certifi==2021.5.30 | |
| cffi==1.14.6 | |
| charset-normalizer==2.0.6 | |
| click==8.0.1 | |
| configparser==5.0.2 | |
| cryptography==3.4.7 | |
| databases==0.5.0 | |
| ecdsa==0.17.0 | |
| eventsourcing==9.1.1 | |
| fastapi==0.68.0 | |
| graphene==2.1.9 | |
| graphql-core==2.3.2 | |
| graphql-relay==2.0.1 | |
| greenlet==1.1.1 | |
| h11==0.12.0 | |
| httptools==0.2.0 | |
| idna==3.2 | |
| iniconfig==1.1.1 | |
| numpy==1.21.2 | |
| packaging==21.0 | |
| pandas==1.3.4 | |
| paramiko==2.7.2 | |
| passlib==1.7.2 | |
| pluggy==1.0.0 | |
| promise==2.3 | |
| psycopg2-binary==2.9.1 | |
| py==1.10.0 | |
| pyasn1==0.4.8 | |
| pycparser==2.20 | |
| pydantic==1.8.2 | |
| PyNaCl==1.4.0 | |
| pyparsing==2.4.7 | |
| pysftp==0.2.9 | |
| pytest==6.2.5 | |
| python-dateutil==2.8.2 | |
| python-dotenv==0.19.0 | |
| python-jose==3.3.0 | |
| python-multipart==0.0.5 | |
| pytz==2021.3 | |
| PyYAML==5.4.1 | |
| requests==2.26.0 | |
| rsa==4.7.2 | |
| Rx==1.6.1 | |
| six==1.16.0 | |
| SQLAlchemy==1.4.23 | |
| starlette==0.14.2 | |
| toml==0.10.2 | |
| typing-extensions==3.10.0.0 | |
| urllib3==1.26.7 | |
| uvicorn==0.15.0 | |
| uvloop==0.16.0 | |
| var-dump==1.2 | |
| watchgod==0.7 | |
| websockets==9.1 |
This file contains hidden or 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
| from typing import Optional | |
| from fastapi import FastAPI | |
| import pysftp | |
| import requests | |
| import json | |
| import requests as req | |
| import datetime | |
| import os | |
| from dotenv import load_dotenv | |
| load_dotenv() | |
| d = datetime.datetime.now() | |
| currentDate = d.strftime("%Y-%m-%d") | |
| API_ENDPOINT = DOMAIN+os.getenv('API_ENDPOINT') | |
| def getData(): | |
| try: | |
| r = req.get(API_ENDPOINT) | |
| if r.status_code==500 or r.status_code==403: | |
| json_data = "" | |
| else: | |
| json_data = r.json() | |
| except Exception as e: | |
| json_data = "" | |
| finally: | |
| return json_data | |
| def getFileName(type): | |
| d = datetime.datetime.now() | |
| if type=='csv': | |
| return (d.strftime("%d-%m-%Y_leadsdata.csv")) | |
| else: | |
| return (d.strftime("%d-%m-%Y_leadsdata.json")) | |
| def removeFile(path): | |
| d = os.unlink(path) | |
| return True |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment