Код: Выделить всё
logging.info(f"Updating list {list["name"]} using session token {token}.")
# Create a temporary CSV file
with tempfile.NamedTemporaryFile(mode="w+", newline="", suffix=".csv", delete=False) as temp_file:
temp_path = temp_file.name
logging.info(f"Using temp file {temp_file.name}.")
# Connect to SQL Server
conn_str = (
f"DRIVER={{{DRIVER}}};"
f"SERVER={list["server"]};"
f"DATABASE={list["database"]};"
"Trusted_Connection=yes;"
"TrustServerCertificate=yes;"
)
logging.info(f"Connecting to SQL Server {conn_str}.")
with pyodbc.connect(conn_str) as conn:
cursor = conn.cursor()
logging.info(f"Executing query '{list["query"]}'.")
cursor.execute(list["query"])
logging.info(f"Writing column headers.")
column_names = [column[0] for column in cursor.description]
writer = csv.writer(temp_file)
writer.writerow(column_names)
logging.info(f"Writing data lines.")
for row in cursor:
writer.writerow(row)
payload={}
files=[('file',(temp_file.name,open(temp_file.name,'rb'),'text/csv'))]
headers = {'X-Gatekeeper-SessionToken': token,'FastField-API-Key': APIKEY}
logging.info(f"Sending list csv file to {list["url"]} using token {token}.")
response = requests.request("PUT", list["url"], headers=headers, data=payload, files=files)
logging.info(f"Received response {response.text}.")
return response.text
# Remove the temp file created above
os.remove(temp_path)
Подробнее здесь: https://stackoverflow.com/questions/798 ... in-windows
Мобильная версия