I have crated a postgreSQL cloud SQL instance in GCP and I have created a user and a DB for it. I can connect to it via cloud_sql_proxy tool:
$ cloud_sql_proxy -instances=project_name:REGION:instance_name=tcp:5432 -credential_file=/path/to/key.json
I then can successfully connect to instance via psql and run queries and insert data etc in command line:
$ psql "host=127.0.0.1 port=5432 sslmode=disable dbname=myDBname user=myUser"
Password:
psql (10.18 (Ubuntu 10.18-0ubuntu0.18.04.1), server 13.3)
WARNING: psql major version 10, server major version 13.
Some psql features might not work.
Type "help" for help.
myDBname=>SELECT * FROM MyTable;
My issue is that when I try to use the sqlalchemy library and use the sample code provided in sqlalchemy example code like this:
import sqlalchemy
import os
db_config = {
"pool_size": 5,
"max_overflow": 2,
"pool_timeout": 30, # 30 seconds
"pool_recycle": 1800, # 30 minutes
}
def init_tcp_connection_engine(db_config):
db_user = "myUser"
db_pass = "myPassword"
db_name = "myDBname"
db_hostname = "127.0.0.1"
db_port = 5432
pool = sqlalchemy.create_engine(
# Equivalent URL:
# postgresql+pg8000://<db_user>:<db_pass>#<db_host>:<db_port>/<db_name>
sqlalchemy.engine.url.URL.create(
drivername="postgresql+pg8000",
username=db_user, # e.g. "my-database-user"
password=db_pass, # e.g. "my-database-password"
host=db_hostname, # e.g. "127.0.0.1"
port=db_port, # e.g. 5432
database=db_name # e.g. "my-database-name"
),
**db_config
)
# [END cloud_sql_postgres_sqlalchemy_create_tcp]
pool.dialect.description_encoding = None
return pool
def main():
db = init_tcp_connection_engine(db_config)
with db.connect() as conn:
rows = conn.execute("SELECT * FROM MyTable;").fetchall()
for row in rows:
print(row)
if __name__ == "__main__":
main()
I get the error of
Exception has occurred: ProgrammingError (note: full exception trace is shown but execution is paused at: <module>)
(pg8000.dbapi.ProgrammingError) {'S': 'FATAL', 'V': 'FATAL', 'C': '28P01', 'M': 'password authentication failed for user "myUser"', 'F': 'auth.c', 'L': '347', 'R': 'auth_failed'}
(Background on this error at: https://sqlalche.me/e/14/f405)
Any idea what is wrong and how I can resolve this?
I changed the password via webUI and then paste it into code and it worked.
Hello I am trying to download youtube videos with the following code
import youtube_dl
import tempfile
youtube_links = ['https://www.youtube.com/watch?v=668nUCeBHyY']
with tempfile.TemporaryDirectory() as tempdir:
opts = {
'format': 'best',
'outtmpl': f'{tempdir}/%(id)s.%(ext)s',
'noplaylist': True,
'postprocessors': [{
'preferredcodec': 'mp4'
}]
}
ydl = youtube_dl.YoutubeDL(opts)
try:
meta = ydl.extract_info(
youtube_links,
download=True
)
except Exception as e:
raise e
else:
print(f"Downloaded to {tempdir}/{meta['id']}.{meta['ext']}")
However this raises an error:
<path>>py -3.8 test.py
Traceback (most recent call last):
File "test.py", line 14, in <module>
ydl = youtube_dl.YoutubeDL(opts)
File "C:\Users\User\AppData\Local\Programs\Python\Python38\lib\site-packages\youtube_dl\YoutubeDL.py", line 429, in __init__
pp_class = get_postprocessor(pp_def_raw['key'])
KeyError: 'key'
And none of the examples told me anything about a key I have to pass, nor can I find anything about it, what am I doing wrong?
In a nutshell, try this:
'postprocessors': [{
'key':'FFmpegMetadata',
'preferredcodec': 'mp4'
}]
There are lots of keys, so if it's not working, check out this: https://github.com/ytdl-org/youtube-dl/blob/master/youtube_dl/postprocessor/__init__.py
The trick is that the keys listed inside the file are not usable because you gotta drop the "PP" part!!
FYI:
It seems you are trying to get video? Then you should try the documented style: https://github.com/ytdl-org/youtube-dl/
You have to look at the "Functions" yourself and guess what parameters you should put here: https://github.com/ytdl-org/youtube-dl/blob/master/youtube_dl/YoutubeDL.py In fact, somewhere in this link says, "check out the init.py for key information."
I am trying to write a code which will read values from excel file and will create VMs in Google Cloud. I am facing problem at two locations, while creating tags if I use 'items': [tag] or while creating service account scope it starts giving me error.
import os, json
import googleapiclient.discovery
from google.oauth2 import service_account
import csv
credentials = service_account.Credentials.from_service_account_file('G:/python/json/mykids-280210.json')
compute = googleapiclient.discovery.build('compute', 'v1', credentials=credentials)
def create_instance(compute, vm_name, image_project, image_family, machinetype, startupscript, zone, network,
subnet, project, scope, tag):
# Get the latest Debian Jessie image.
image_response = compute.images().getFromFamily(
project=image_project, family=image_family).execute()
source_disk_image = image_response['selfLink']
# Configure the machine
machine_type = "zones/" + zone + "/machineTypes/" + machinetype
startup_script = startupscript
config = {
'name': vm_name,
'machineType': machine_type,
'description': 'This VM was created with python code',
'tags': {
'items': ['external', 'home', 'local'] #'items': [tag] <~~~~~~~~~~~
},
'deletionProtection': False,
'labels': {'env': 'dev', 'server': 'mytower', 'purpose': 'personal'},
# Specify the boot disk and the image to use as a source.
'disks': [
{
'boot': True,
'autoDelete': True,
'initializeParams': {
'sourceImage': source_disk_image,
}
}
],
# Specify a network interface with NAT to access the public
# internet.
'networkInterfaces': [{
'network': 'global/networks/' + network,
'subnetwork': 'regions/us-central1/subnetworks/' + subnet,
'accessConfigs': [
{'type': 'ONE_TO_ONE_NAT', 'name': 'External NAT'}
]
}],
# Allow the instance to access cloud storage and logging.
'serviceAccounts': [{
'email': 'default',
'scopes': [
#'https://www.googleapis.com/auth/devstorage.read_write', 'https://www.googleapis.com/auth/logging.write'
#scope # scope <~~~~~~~~~~~~~~~~~~~~
]
}],
'scheduling': {
"preemptible": True
},
# Metadata is readable from the instance and allows you to
# pass configuration from deployment scripts to instances.
'metadata': {
'items': [{
# Startup script is automatically executed by the
# instance upon startup.
'key': 'startup-script',
'value': startup_script
}]
}
}
return compute.instances().insert(
project=project,
zone=zone,
body=config).execute()
# [END create_instance]
with open('vms.csv', newline='') as csvfile:
data = csv.DictReader(csvfile)
for row in data:
vm_name = row['vm_name']
image_project = row['image_project']
image_family = row['image_family']
machinetype = row['machinetype']
startupscript = row['startupscript']
zone = row['zone']
network = row['network']
subnet = row['subnet']
project = row['project']
scope = row['scopes']
tag = row['tags']
print(create_instance(compute, vm_name, image_project, image_family, machinetype, startupscript, zone, network,
subnet, project, scope, tag))
csvfile.close()
error when use scope variable
G:\python\pythonProject\venv\Scripts\python.exe G:/python/pythonProject/read-excel-gcp/vm/create_vm.py
Traceback (most recent call last):
File "G:\python\pythonProject\read-excel-gcp\vm\create_vm.py", line 100, in <module>
print(create_instance(compute, vm_name, image_project, image_family, machinetype, startupscript, zone, network,
File "G:\python\pythonProject\read-excel-gcp\vm\create_vm.py", line 79, in create_instance
return compute.instances().insert(
File "G:\python\pythonProject\venv\lib\site-packages\googleapiclient\_helpers.py", line 134, in positional_wrapper
return wrapped(*args, **kwargs)
File "G:\python\pythonProject\venv\lib\site-packages\googleapiclient\http.py", line 915, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 400 when requesting https://compute.googleapis.com/compute/v1/projects/mykids-280210/zones/us-central1-a/instances?alt=json returned "One or more of the service account scopes are invalid: 'https://www.googleapis.com/auth/devstorage.read_write', 'https://www.googleapis.com/auth/logging.write''". Details: "One or more of the service account scopes are invalid: 'https://www.googleapis.com/auth/devstorage.read_write', 'https://www.googleapis.com/auth/logging.write''">
Process finished with exit code 1
I get a similar error when I use tag variable.
I have # the value the way I am passing in the above code.
Below is my csv file details
vm_name,image_project,image_family,machinetype,startupscript,zone,network,subnet,project,scopes,tags
python-vm1,debian-cloud,debian-9,e2-micro,G:/python/json/startup-script.sh,us-central1-a,myvpc,subnet-a,mykids-280210,"https://www.googleapis.com/auth/devstorage.read_write', 'https://www.googleapis.com/auth/logging.write'","external', 'home', 'local'"
python-vm2,debian-cloud,debian-9,e2-micro,G:/python/json/startup-script.sh,us-central1-a,myvpc,subnet-a,mykids-280210,"https://www.googleapis.com/auth/devstorage.read_write', 'https://www.googleapis.com/auth/logging.write'","external', 'home', 'local'"
I am not sure that when the value are passed directly it works, but when passing the value through variable, it fails.
I have marked the problem area with <~~~~~~~~~~~~
Please suggest if anyone understands the issue.
#d.s can you try changing your scope format to something like this:
'serviceAccounts': [
{
'email': 'default'
'scopes':[
'https://www.googleapis.com/auth/compute',
'https://www.googleapis.com/auth/servicecontrol',
'https://www.googleapis.com/auth/service.management.readonly',
'https://www.googleapis.com/auth/logging.write',
'https://www.googleapis.com/auth/monitoring.write',
'https://www.googleapis.com/auth/trace.append',
'https://www.googleapis.com/auth/devstorage.read_write']}]
The listed scopes are the default scopes that you will need for an instance. I think the problem you are facing is you where trying to only list two scopes which are not enough to allow you to deploy your instance.
# -*- coding: utf-8 -*-
{
'name': "myfirstModel",
'summary': """
Short (1 phrase/line) summary of the module's purpose, used as
subtitle on modules listing or apps.openerp.com""",
'description': """
Long description of module's purpose
""",
'author': "My Company",
'website': "http://www.yourcompany.com",
# Categories can be used to filter modules in modules listing
# Check https://github.com/odoo/odoo/blob/master/odoo/addons/base/module/module_data.xml
# for the full list
'category': 'Uncategorized',
'version': '0.1',
# any module necessary for this one to work correctly
'depends': ['base'],
# always loaded
'data': [
# 'security/ir.model.access.csv',
'views/views.xml',
'views/templates.xml',
],
# only loaded in demonstration mode
'demo': [
'demo/demo.xml',
],
}
i got this error while creating a new module in odoo... i am unable to import 'loglevels' in pycharm...?
any help is appreciated...
I am new to python. I am writing a code to make changes remotely to cisco switches and my code is as follows:
from netmiko import ConnectHandler
cisco_sw = {
'device_type': 'cisco_ios',
'ip': '10.18.120.3',
'username': 'cisco',
'password': 'cisco',
'port' : 22, # optional, defaults to 22
'secret': '', # optional, defaults to ''
'verbose': False, # optional, defaults to False
}
net_connect = ConnectHandler(**cisco_sw)
net_connect.find_prompt() # to verify if connection is established
config_commands = [ 'commands']
output = net_connect.send_config_set(config_commands)
print(output)
net_connect.exit_config_mode()
output = net_connect.send_command_expect('write memory')
print(output)
output = net_connect.disconnect()
I wanted to modify the code so that "ip" in cisco_sw dictionary and "commands" inputted in config_commands can be directly inputted from text file. Please help me to know how to input one line at a time from text file during every iteration.