rest server centos 7 and python 3 doesnt seem work well - linux

I have a rest server that i use it in the my main program ; rest server is running on centos 7 vps when i run the main on my laptop which is windows 7 it works as well; but when i run the main as a service on the same vps server it doesn't work well i've checked every single possible problems even changing unicode but i wouldn't find a solution yet.
here is my rest server code:
from bottle import route, run, redirect
import sqlite3
db = sqlite3.connect("checkLink.db")
db.execute("DROP TABLE IF EXISTS checkLink")
db.execute("CREATE TABLE checkLink(user_id INT, link TEXT, join_link TEXT,
check_id INT)")
db.execute("CREATE UNIQUE INDEX user_id ON checkLink(user_id, link,
join_link, check_id)")
db.commit()
def database(user_id, link):
db2 = sqlite3.connect("checkLink.db")
db2.row_factory = sqlite3.Row
cursor = db2.execute("SELECT * FROM checkLink WHERE user_id=? AND link=?",
(user_id, link))
db2.commit()
for row in cursor:
check_id = row["check_id"]
if check_id == 1:
return 1
else:
return 0
def database_insert(user_id, link):
db2 = sqlite3.connect("checkLink.db")
db2.execute("INSERT INTO checkLink(user_id, link, join_link, check_id)
VALUES (?,?,?,?)", (user_id, link,
"http://my link", 1))
db2.commit()
#route('/<link>/<chat_id>')
def index(link, chat_id):
check = database(chat_id, link)
if check != 1:
database_insert(chat_id, link)
redirect("https://my link")
#route('/redirect')
def index():
redirect("https://my link")
#route('/check/<link>/<chat_id>')
def index(link, chat_id):
check = database(chat_id, link)
if check == 1:
return {"id": chat_id, "permission": "True"}
else:
return {"id": chat_id, "permission": "False"}
run(host='my ip', port=my port)

Related

Save all data in database with one query in Django

I'm a new Django programmer. I write a API call with rest_framework in Django. when call this API, my program connect to KUCOIN and get list of all cryptocurrency. I want save symbol and name this cryptocurrencies in database. For save data to database, I use 'for loop' and in every for loop iteration, I query to database and save data. my code :
for currencie in currencies:
name = currencie['name']
symbol = currencie['symbol']
active = (False, True)[symbol.endswith('USDT')]
oms = 'kucoin'
try:
obj = Instrument.objects.get(symbol=symbol, oms=oms)
setattr(obj, 'name', name)
setattr(obj, 'active', active)
obj.save()
except Instrument.DoesNotExist:
obj = Instrument(name=name, symbol=symbol,
active=active, oms=oms)
obj.save()
query to database in every for loop iteration have problem ,How can I solve this problem?
Exist any way in Django to save data in database in with one query.
All my code:
class getKucoinInstrument(APIView):
def post(self, request):
try:
person = Client.objects.filter(data_provider=True).first()
person_data = ClientSerializer(person, many=False).data
api_key = person_data['api_key']
api_secret = person_data['secret_key']
api_passphrase = person_data['api_passphrase']
client = kucoin_client(api_key, api_secret, api_passphrase)
currencies = client.get_symbols()
for currencie in currencies:
name = currencie['name']
symbol = currencie['symbol']
active = (False, True)[symbol.endswith('USDT')]
oms = 'kucoin'
try:
obj = Instrument.objects.get(symbol=symbol, oms=oms)
setattr(obj, 'name', name)
setattr(obj, 'active', active)
obj.save()
except Instrument.DoesNotExist:
obj = Instrument(name=name, symbol=symbol,
active=active, oms=oms)
obj.save()
return Response({'response': 'Instruments get from kucoin'}, status=status.HTTP_200_OK)
except Exception as e:
print(e)
return Response({'response': 'Internal server error'}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
Thank you for you help.
Yes! Take a look at bulk_create() documentation. https://docs.djangoproject.com/en/4.0/ref/models/querysets/#bulk-create
If you have a db that supports ignore_conflicts parameter (all do, except Oracle), you can do this:
new_currencies = []
for currencie in currencies:
name = currencie['name']
symbol = currencie['symbol']
active = (False, True)[symbol.endswith('USDT')]
oms = 'kucoin'
new_currencies.append(Instrument(name=name, symbol=symbol,
active=active, oms=oms))
Instrument.objects.bulk_create(new_currencies, ignore_conflicts=True)
1-liner:
Instrument.objects.bulk_create(
[
Instrument(
name=currencie['name'], symbol=currencie['symbol'],
active=currencie['symbol'].endswith('USDT'), oms='kucoin'
)
for currencie in currencies
],
ignore_conflicts=True
)

relation does not exist in postgreSQL but already exist

I've read a lot of articles about my problem but no one solve it. So u can see my code here
DATABASE_URL = os.environ.get('url_of_my_db')
con = None
try:
con = psycopg2.connect(DATABASE_URL)
cur = con.cursor()
print('PostgreSQL database version:')
#cur.execute('SELECT version()')
#cur.execute('SELECT * FROM qwerty')
#cur.execute(sql.SQL('SELECT * FROM {}').format(sql.Identifier('qwerty')))
#cur.execute(sql.SQL("INSERT INTO {} (chat_id, username, created_on) VALUES (8985972942, vovakirdan, 2022-01-05)").format(sql.Identifier('users')))
cur.execute("""INSERT INTO users (chat_id, username, created_on)
VALUES (3131,
vovakirdan,
2022-01-05)""")
# display the PostgreSQL database server version
db_version = cur.fetchone()
print(db_version)
# close the communication with the HerokuPostgres
cur.close()
except Exception as error:
print('Cause: {}'.format(error))
finally:
# close the communication with the database server by calling the close()
if con is not None:
con.close()
print('Database connection closed.')
and in my DB table named "users" (named without quotes) are exist, but I still have this error:
error
...relation "users" does not exist
all the #commented code doesn't work and send the same error besides SELECT version(), it works perfectly that proves that connection works.
The problem was that PostgreDB wants me to use SELECT colum FROM schema.table instead of SELECT colum FROM table. And that's all. Thanks everyone

Error while fetching data from PostgreSQL column "user2" does not exist

My Django views.py passes a email address to a python function. The
python function queries a PostgreSQL table using a where clause. The
where clause is equal to the email address that was passed in
(someuser). My goal is to retrieve the userid value for this email
address. But I get an error message telling me that there is no column
called "user2" which is correct (there is not). But why does the
select see that as an existing column?
I print the email out only to verify that it got passed in. someuser
for select= user2#gmail.com
Error while fetching data from PostgreSQL
column "user2" does not exist LINE 1: SELECT userid from accounts_user
WHERE email = user2#gmail.c...
I have ran this locally on my Windows 10 box as well as on AWS Cloud9
with the same result. It tells me that no column exists that begins
with the email address up to the '#' sign.
import psycopg2
def get_userid(someuser):
try:
connection = psycopg2.connect(bla, bla")
cursor = connection.cursor()
print("someuser for select= ", someuser)
postgreSQL_select_Query = ("""SELECT userid from accounts WHERE email = %s;""" %(someuser))
cursor.execute(postgreSQL_select_Query)
mobile_records = cursor.fetchall()
for row in mobile_records:
print("userid = ", row[0], )
except (Exception, psycopg2.Error) as error :
print ("Error while fetching data from PostgreSQL", error)
finally:
if(connection):
cursor.close()
if __name__ == '__main__':
get_userid()
table has this:
id | email | userid
----+------------------------+--------
18 | user2#gmail.com | u48923
I expect to get the userid of "u48923" after running the select.
Change it to the following
postgreSQL_select_Query = ("""SELECT userid from accounts WHERE email = '%s';""" %(someuser))
But a better way is to use the built in functionality of psycopg2 and write it as follows
cursor.execute("""SELECT userid from accounts WHERE email = %s;""", (someuser,))
Psycopg2 Documentation
def getEmail(someuser): print("received someuser in 'run_select' from views.py= ", someuser) try:
connection = psycopg2.connect(user="my user",
password="xxxxxx",
host="127.0.0.1",
port="5432",
database="my_db")
cursor = connection.cursor()
print("again someuser for select= ", someuser)
someuser = str(someuser) # convert to string or you get: Error while fetching data from PostgreSQL can't adapt type 'User'
print("someuser= ", someuser)
cursor.execute("""SELECT userid from accounts_user WHERE email = %s""", (someuser,))
print("Selecting rows from c9_max_forms_kw using cursor.fetchall")
mobile_records = cursor.fetchall()
print("Print each row and it's columns values")
for row in mobile_records:
print("Id = ", row[0], )
#print("email = ", row[5])
#print("userid = ", row[9], "\n") # should be "u48923"
#save = ''.join(row[9]) # convert this tuple to a string except (Exception, psycopg2.Error) as error :
print ("Error while fetching data from PostgreSQL", error) finally:
#closing database connection.
if(connection):
cursor.close()
connection.close()
print("PostgreSQL connection is closed") return someuser
Joshua is the man. The string conversion was the key. The error given without it doesn't seem to make sense to me. Best to all. Ken.

Problem reading sql query in python 3 with pandas

Good morning.
I'm trying to read a SQL query with pandas through a SSH tunnel. It worked fine in python 2.7, but now, with python 3.7, it seems like the process paused when executing pd.read_sql_query. My code is the following:
def conect(lista, names):
# Block of code where I set used variables below.
while not success and retries < max_retries:
try:
print('Trying to connect ({n})...'.format(n = retries + 1))
with SSHTunnelForwarder((REMOTE_SERVER, 22),
ssh_username = user_name,
ssh_pkey = ssh_pkey,
ssh_private_key_password= password,
remote_bind_address=(str_number, PUERTO),
local_bind_address=('', PUERTO)) as tunnel:
engine = sqlalchemy.create_engine("postgresql+psycopg2://{user}:{passw}#{host}:{port}/{db}".format(
user = user_name,
passw = long_pass,
host = tunnel.local_bind_host,
port = tunnel.local_bind_port,
db = db))
dic_df = {name: pd.read_sql_query(query, engine) for query, name in zip(lista, names)}
return dic_df
except Exception as e:
print('Error...')
print(e)
retries += 1
I don't know if the problem comes from the engine or from the function pd.read_sql_query itself...
Many thanks in advance!!

Passing input into Curl command inside python3

I'm currently working with errbot, but i'm having trouble with allowing users to enter a message to be passed along with the curl command. my plugin looks as follows:
#arg_botcmd('team_key', type=str)
#arg_botcmd('--message' , dest='message', type=str)
def oncall_page(self, msg, team_key=None, message=None):
if team_key in page_list.keys():
team_id = page_list[team_key]
data = {"message_type":"CRITICAL","state_message":"{0}".format(message)}
response = requests.post('https://www.apiurl.com/{0}'.format( team_id), data)
yield "Paging {0} ".format( team_id )
My issue is with this line:
data = {"message_type":"CRITICAL","state_message":"{0}".format(message)}
This seems to be crashing the command completely, I'm hoping users can execute one command such as "!oncall page team_name --message "
Any help would be appreciated:)
#arg_botcmd('team_key', type=str)
#arg_botcmd('--message' , dest='message', type=str)
def oncall_page(self, msg, team_key=None, message=None):
if team_key in page_list.keys():
team_id = page_list[team_key]
text = str(message)
msg_type = "critical"
data = '{"message_type":"%s", "state_message":"%s"}' % (msg_type, text)
# data = '{"message_type":"critical", "state_message":"%s"}'(text)
URL = 'https://www.apiurl.com/{0}'.format( team_id)
response = requests.post(URL , data)
This is the fix for this!

Resources