I created member interface in Python. How can I connect this code to SQL server or do you have both SQL and Python code running dynamically?
ad = {'Ali':'Rıza'}
def kayit(gelen_ad,gelen_soyad):
ad[gelen_ad] = gelen_soyad
print((""""Adınız {} Soyadınız {} olarak kayıt oldunuz.
""").format(gelen_ad,gelen_soyad))
sor = input("Giriş yapmak istermisiniz? y/n >>>")
if sor=='y':
giris_kontrol(gelen_ad,gelen_soyad)
else:
exit()
def giris_kontrol(gelenad,gelensoyad):
s_d = False
for i in ad:
ad_g = i
soyad_g = ad[i]
if gelensoyad==soyad_g and gelenad==ad_g:
print(("""
Giriş başarılı oturum açıldı
Hoşgeldiniz {} {} Bey
""").format(ad_g,soyad_g))
s_d= True
if s_d==False:
print("""Hesap bulunamadı,Kayıt olmak istermisiniz?
y/n
""")
sor = input(">>>")
if sor == 'y':
ad_Sor = input("""
İsim:
""")
soyad_Sor = input("""
Soyad:
""")
kayit(ad_Sor,soyad_Sor)
else:
print("Çıkılıyor")
exit()
def kullanici():
ad = input('Adınız >>>')
soyad = input('Soyadınız >>>')
giris_kontrol(ad,soyad)
kullanici()
I created an interface to become a member and login. It did, but I can't provide SQL connection.
Related
The script runs perfectly fine in VS Code, but cronjob fails.. However, after I run code in VS Code, and then run the cronjob again, cron works fine.. Really not sure what I am missing..
It seems like
media = self.api.media_upload(filename=media_upload[i])
didn't run properly.
my_tweet.py
import keyring
import tweepy as tw
class Tweet:
def __init__(self):
#self.consumer_key = keyring.get_password("PY_INFO", "tw_consumer_key")
#self.consumer_secret = keyring.get_password("PY_INFO", "tw_consumer_secret")
#self.access_token = keyring.get_password("PY_INFO", "tw_access_token")
#self.access_token_secret = keyring.get_password("PY_INFO", "tw_access_token_secret")
self.consumer_key = '...'
self.consumer_secret = '...'
self.access_token = '...'
self.access_token_secret = '...'
self.auth = tw.OAuth1UserHandler(
self.consumer_key,
self.consumer_secret,
self.access_token,
self.access_token_secret
)
self.api = tw.API(self.auth)
def post(self,status,in_reply_to_status_id=None,media_upload=None):
try:
print(media_upload)
if not media_upload == None:
media_ids = []
print("upload media...")
if isinstance(media_upload, list):
for i in range(len(media_upload)):
print(media_upload[i])
media = self.api.media_upload(filename=media_upload[i])
print(f'added media {media_upload[i]}.....')
media_ids.append(media.media_id_string)
print(f'appended media_id_string.....')
else:
media = self.api.media_upload(filename=media_upload)
media_ids.append(media.media_id_string)
print("before update status: ")
my_tweet = self.api.update_status(status=status,
media_ids= media_ids,
in_reply_to_status_id=in_reply_to_status_id)
else:
my_tweet = self.api.update_status(status=status,
in_reply_to_status_id=in_reply_to_status_id)
return my_tweet.id_str
except:
print(f"Tweet: {status} - Failed")
tw = my_tweet.Tweet()
medias = []
for i in MEDIAS_TO_ADD:
print(i)
p = tr.get_priv_data_path(i,date=date)
assert len(p) > 0
medias.append(p)
tw.post(f"{date} - Market Internals",media_upload=medias)
Hello I am new one for python and telegram api so i have some questions. I am creating telegram bot(python telegram api) with users profile. I have created database(mysql.connector) and store there all users info after registration. Also i have created user class. When user types /start i am checking if it exists, if it is, i am filling this class. Then i use this class to show some profile information(photo, name, age, etc.) if users click on button(my profile). So the problem is when i have 2-users at the same time. First typed "/start" and logged in, wanna watch self profile, everything fine. But when second user do the same, i got that the first users when clicked on (my profile), he or she got the last one profile who typed "/start" loaded for both users. How to fix this? Solution to check and load data all the time sounds not good, i'd like to do smth with "class Users", but i don't know to make it uniq for each users session. Any solutions? If it's needed i can give more code, just ask.
class Users:
def __init__(self, id=0, name='', age=0, gender='', balance=0, telegram_id=0, photo='', sallarytext=0, sallaryvideo=0, videocall=0):
self.id = id
self.name = name
self.age = age
self.gender = gender
self.balance = balance
self.telegram_id = telegram_id
self.photo = photo
self.sallarytext = sallarytext
self.sallaryvideo = sallaryvideo
self.videocall = videocall
user = Users()
def check_auth(connection, telegram_id):
cursor = connection.cursor()
result = None
try:
cursor.execute("SELECT * FROM users WHERE telegram_id = '%s'" % telegram_id)
result = cursor.fetchall()
data = []
if result:
for row in result:
user.id = row[0]
user.name = row[1]
user.age = row[2]
user.gender = row[3]
user.telegram_id = row[4]
user.balance = row[5]
data = [user.name]
if user.gender == 'Female':
cursor.execute("SELECT * FROM photos WHERE users_id = '%s'" % user.id)
result2 = cursor.fetchall()
for row in result2:
user.photo = row[1]
user.sallarytext = row[2]
user.sallaryvideo = row[3]
user.videocall = row[4]
return data
except Error as e:
print(f"The error '{e}' occurred")
#bot.message_handler(commands=['start'])
def check_reg(message):
if message.chat.type == 'private':
telegram_id = message.from_user.id
# create_db_users(connection)
# create_db_photos(connection)
# create_db_chats(connection)
data_user = check_auth(connection, telegram_id)
if not data_user:
new_user(message) # user registration
else:
if user.gender == 'Male':
default_user_keybord(message) # show user keybord
elif user.gender == 'Female':
default_model_keybord(message)
def show_profile(message): # funtion show profile when user click on "My profile" button
profile_text = "Profile\n\nYour name: " + user.name + "\nYour age: " + str(
user.age)
menu_keybord = types.ReplyKeyboardMarkup(row_width=2, resize_keyboard=True)
button_name_age = types.KeyboardButton(text="🗣 Change name/age")
button_back = types.KeyboardButton(text="◀️ Return")
menu_keybord.add(button_name_age, button_back)
bot.send_message(message.chat.id, profile_text, reply_markup=menu_keybord)
Could you tell me what telegram api package you are using exactly?
The core of your problem, I think, is the use of a global variable user to store user data. It would be best practice to instantiate and return a new Users every time you call check_auth.
That being said,
in Python, if you want to update a global variable, say user, you have to use the statement global user before you do so;
consider using an ORM such as SQLAlchemy to spare you some headaches and code.
Let me know if that solved your issue.
C
I have fixed this by doing request to the database, getting data and pushing it to class, then closing connection with database and showing to user
The following is a slightly censcored Python 3.7 program which apparently goes to sleep (0% cpu but still there according to htop) after a couple hours of running on Linux on my RaspberryPi. I've programmed it to react to STRG+C, after going to sleep, it still reacts to STRG+C, but I guess that still means that it's firmly asleep. It hasn't gained any memory since it started, still at 22MB,
also I've used a profiler to check that there aren't any memory leaks whatsoever.
The cursor is at the next line and the program just doesn't react anymore...
import mysql.connector, time, signal, requests
from mysql.connector import errorcode
USER = ...
PASSWORD = ...
HOST = ...
DATABASE = ...
RUNNING = True
def signal_handler(signum, frame):
global RUNNING
signal.signal(signum, signal.SIG_IGN) # ignore additional signals
print('Received shutdown signal')
print('Treating remaining players as if they just went offline...')
RUNNING = False
def time_string(timestamp, forString = False, brackets = False):
hours = int((timestamp // 3600 + (0 if forString else 2)) % 24)
minutes = int(timestamp // 60 % 60)
seconds = int(timestamp % 60)
shours = ('0' if hours < 10 else '') + str(hours)
sminutes = ('0' if minutes < 10 else '') + str(minutes)
sseconds = ('0' if seconds < 10 else '') + str(seconds)
ret = shours+':'+sminutes+':'+sseconds
if brackets:
ret = '['+ret+']'
return ret
def main():
global USER, PASSWORD, HOST, DATABASE
start_timestamp = None
end_timestamp = None
registeredWhen = dict()
startedOnline = dict()
startedPlaying = dict()
try:
conn = mysql.connector.connect(user=USER, password=PASSWORD, host=HOST, database=DATABASE)
except mysql.connector.Error as err:
if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
print("Something is wrong with your user name or password")
elif err.errno == errorcode.ER_BAD_DB_ERROR:
print("Database does not exist")
else:
print(err)
conn.close()
return
conn.autocommit = True
cursor = conn.cursor(prepared=True)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS recording_periods(
player TEXT,
start INTEGER,
end INTEGER
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS online_periods(
player TEXT,
start INTEGER,
end INTEGER
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS playing_periods(
player TEXT,
start INTEGER,
end INTEGER
)
"""
)
cursor.execute(
"""
CREATE TABLE IF NOT EXISTS players_to_record(
player TEXT
)
"""
)
signal.signal(signal.SIGINT, signal_handler)
last_timestamp = time.time()-1
timestamp = last_timestamp
start_timestamp = None
end_timestamp = None
while RUNNING or [] != [x for x in startedOnline if startedOnline[x] != None] or [] != [x for x in startedPlaying if startedPlaying[x] != None]:
timestamp = int(time.time())
while timestamp == last_timestamp:
time.sleep(0.01)
timestamp = int(time.time())
cursor.execute("""SELECT player FROM players_to_record""")
nplayers = set([x[0].decode().lower() for x in cursor.fetchall()])
added = set([player for player in nplayers if player not in registeredWhen])
removed = set([player for player in registeredWhen if player not in nplayers])
for player in added:
print('Registered player '+player)
registeredWhen[player] = timestamp
startedOnline[player] = None
startedPlaying[player] = None
info = []
for player in removed:
print('Unregistered player '+player)
info.append((player, registeredWhen[player], end_timestamp))
registeredWhen.pop(player)
startedOnline.pop(player)
startedPlaying.pop(player)
cursor.executemany("""INSERT INTO recording_periods VALUES (?, ?, ?)""", info)
url = 'https://lichess.org/api/users/status?ids='
for player in registeredWhen:
url = url + player +','
url = url[:-1]
try:
resp = requests.get(url)
data = resp.json()
resp.close()
ts = time_string(timestamp, brackets=True)
tsnb = time_string(timestamp, brackets=False)
ronline = []
rplaying = []
for user in data:
name_id = user['id']
name = user['name']
online = 'online' in user and user['online']
playing = 'playing' in user and user['playing']
if start_timestamp == None:
start_timestamp = timestamp
end_timestamp = timestamp
if startedOnline[name_id] == None and online and RUNNING:
startedOnline[name_id] = timestamp
print(ts,name,'went online')
elif startedOnline[name_id] != None and ((not online) or not RUNNING):
ronline.append((name_id, startedOnline[name_id], timestamp))
st = time_string(startedOnline[name_id])
dt = time_string(timestamp - startedOnline[name_id], forString=True)
print(ts,name,'went offline, was online from',st,'to',tsnb,'for',dt)
startedOnline[name_id] = None
if startedPlaying[name_id] == None and playing and RUNNING:
startedPlaying[name_id] = timestamp
print(ts,name,'started playing')
elif startedPlaying[name_id] != None and ((not playing) or not RUNNING):
rplaying.append((name_id, startedPlaying[name_id], timestamp))
st = time_string(startedPlaying[name_id])
dt = time_string(timestamp - startedPlaying[name_id], forString=True)
print(ts,name,'stopped playing, was playing from',st,'to',tsnb,'for',dt)
startedPlaying[name_id] = None
cursor.executemany("""
INSERT INTO online_periods
VALUES(?, ?, ?)
""", ronline
)
cursor.executemany("""
INSERT INTO playing_periods
VALUES(?, ?, ?)
""", rplaying
)
except Exception as ex:
template = "An exception of type {0} occurred. Arguments:\n{1!r}"
message = template.format(type(ex).__name__, ex.args)
print (message)
time.sleep(1)
st = time_string(start_timestamp)
et = time_string(end_timestamp)
dt = time_string(end_timestamp - start_timestamp, forString=True)
print('Recorded from',st,'to',et,'for',dt)
print('Writing recording periods to the database...')
cursor.executemany("""
INSERT INTO recording_periods
VALUES(?, ?, ?)
""", [(player, registeredWhen[player], end_timestamp) for player in registeredWhen]
)
print('Committing ...')
conn.commit()
print('Closing database connection...')
cursor.close()
conn.close()
print('Exit program...')
if __name__ == '__main__':
main()
Could it be the terminal? That it's not suited for running for long hours?
But I'd like to use a terminal.
Since my program prints when encountering an error but the terminal doesn't print any error messages,
my guess would be that it's either stuck in the database queries or the requests at Lichess.org.
Our router/Lichess restart from time to time, the databaseServer is up non-stop and has no other program accessing it (which might block the programs query). It's a MariaDB running on the same Raspberry Pi.
I've no idea how and why it gets stuck, please tell me that you guys do!
Thanks in advance!
I've figured it out now,
it's because there's no timeout in requests.get() be default.
When the router restarts while your requests.get() is running, it'll just wait forever.
Fixed it by adding timeout=10. What were the creators of that library thinking??
#app.route("/callback", methods=['POST'])
def callback():
signature = request.headers['X-Line-Signature']
body = request.get_data(as_text=True)
app.logger.info("Request body: " + body)
try:
handler.handle(body, signature)
except InvalidSignatureError:
abort(400)
return 'ok'
def over_time(count):
scope = ["https://spreadsheets.google.com/feeds"]
creds = ServiceAccountCredentials.from_json_keyfile_name('auth.json', scope)
client = gspread.authorize(creds)
sheet = client.open('test').sheet1
content = sheet.cell(1,count).value
return content
#extract the questions from here
def write_in(answers):
scope = ["https://spreadsheets.google.com/feeds"]
creds = ServiceAccountCredentials.from_json_keyfile_name('auth.json', scope)
client = gspread.authorize(creds)
sheet = client.open('test').sheet1
sheet.insert_row(answers, 2)
# write users' answers into the sheet
users=[]
#handler.add(MessageEvent, message=TextMessage)
def handle_message(event):
global users
print("event.reply_token:", event.reply_token)
print("event.message.text:", event.message.text)
user_id = json.loads(str(event.source))['userId']
user_data ={}
location =0
count =0
answers =[]
for user in users:
if user_id==user['user_id']:
user_data =user
count =user_data['count']
location = [i for i, t in enumerate(users) if t['user_id'] == user_id][0]
answers = user_data['answers']
print(answers)
if user_data=={}:
user_data = {"user_id": user_id, "count": 0, "answers":[]}
users.append(user_data)
location=len(users)-1
if event.message.text == "a":
count=1
content = over_time(count)
users[location]['count'] = 1
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=content))
return 0
if event.message.text == "b":
buttons_template = TemplateSendMessage(
alt_text='b template',
template=ButtonsTemplate(
title='type',
text='please choose',
thumbnail_image_url='xxxx',
actions=[
MessageTemplateAction(
label='a',
text='a'
),
]
)
)
line_bot_api.reply_message(event.reply_token, buttons_template)
return 0
if event.message.text != "a" and event.message.text != "b":
answers.append(event.message.text)
count += 1
if count>3:
b=write_in(answers)
print(b)
users[location]['count'] = 0
users[location]['answers'] = []
return 0 #
content = over_time(count)
users[location]['count'] = count
line_bot_api.reply_message(
event.reply_token,
TextSendMessage(text=content))
I actually know how to write them into google sheet. It's just when I tried to interact with users, sometimes it's acting weird. Everytime when a user finished answering the question, we will write its answer to the google sheet then set in blank again so the user can use it next time and fill a new answer. But I got error since some times line-bot would ask me Q1 twice or Q2 twice and didn't successfully write into google sheet. I think the error is mainly in the last part when count>3, I need to stop the whole process and not let it continue.
Steps when you use the line-bot:
write b -> press button a in the form -> Q1 -> A1 -> Q2 -> A2 -> Q3 -> A3 -> if count>3 -> answer=[A1,A2,A3] write into Google sheet -> Set the user data blank ->stop (then user can use it again)
I am writing a python code to be able to add a new user account on LDAP.. Pyhton3 and LDAP3.. so far i can search the data out .. but i can not insert a new user into LDAP via script.. But same account i can insert by using fusiondirectoy.
Issue: it said i have no right when i use ObjectClass : inetOrgPerson. And i need to add givenName attribute which need to use this objectClass.
My code:
import boto3
import sys,getopt
import ldap3
def main(argv):
add_uid = ''
add_username = ''
add_password = ''
add_group = ''
add_mail = ''
add_role = ''
actual_uid = ''
actual_user = ''
actual_pass = ''
actual_group = ''
actual_mail = ''
actual_role = ''
try:
opts, args = getopt.getopt(argv, "hi:u:p:r:e:",["add_uid=","add_username=","add_password=","add_mail="])
except getopt.GetoptError:
PrintHelp()
for opt, arg in opts:
if opt == '-h':
PrintHelp()
elif opt in ("-i", "--add_uid"):
actual_uid = arg
elif opt in ("-u", "--add_username"):
actual_user = arg
elif opt in ("-p", "--add_password"):
actual_pass = arg
elif opt in ("-e", "--add_mail"):
actual_mail = arg
if (actual_uid == '') or (actual_user == '') or (actual_pass == '') or (actual_mail == ''):
PrintHelp()
else:
LDAPfuction(actual_uid,actual_user,actual_pass,actual_mail)
def PrintHelp():
print ('\n### 4 parameters are needed ###: python3 LDAPuserManagement.py -i {} -u {} -p {} -e {} \nExample --> LDAPuserManagement.py -i sumana_w -u "Sumana Wongrattanakul" -p XXXXXXXXXX -e *******#XXXXXX \n-i = user id \n-u = usamename (that want to be added.. format = "FirstName Lastname") \n-p = Password\n-e = targeted user email (xxxxxx#acommerce.asia) \n\nOPTIONALS:\n\n-h = help (how to use)\n')
sys.exit(2)
def LDAPfuction(actual_uid,actual_user,actual_pass,actual_mail):
from ldap3 import Server, Connection, ALL
ldap_server ='ldap://[server]:389'
ldap_base_user = 'ou=people,dc=xxxxx,dc=xxxxxx,dc=xxxxx'
ldap_user = 'user'
ldap_password = 'password'
user_dn = "uid="+ldap_user+","+ldap_base_user
searchFilter_user = "(&(cn="+actual_user+")(objectclass=*))"
search_att_user = ['uid','cn','sn','fdPrivateMail','givenName']
l = ''
conn_user= []
c = ''
lastname = []
dn = ''
attrs = {}
result_add = ''
attrs_1 = {}
try:
#--------------- Connecting to LDAP ---------------------------------------
c=Server(ldap_server, port=389, get_info=ALL)
l=Connection(c,user=user_dn,password=ldap_password,auto_bind=True)
print (l.bind)
if not l.bind():
print ("ERROR in Blind", l.result)
except:
print ("\n\n #### Please open DEV VPN connection #### \n\n")
#--------------- Checking if account is already existing or not -----------
l.search(ldap_base_user, searchFilter_user, attributes=search_att_user)
conn_user =l.entries
if conn_user != []:
print (conn_user)
print ("\n\n ### This account already exist in LDAP ###\n\n")
sys.exit(2)
else:
lastname = str.split(actual_user)
print (actual_uid)
print (actual_user)
print (actual_pass)
print (lastname[0])
print (lastname[1])
print (actual_mail)
dn_1 = "cn="+actual_user+","+ldap_base_user
#dn_1 = "uid="+actual_uid+","+ldap_base_user
attrs['objectclass']=['inetOrgPerson']
attrs['cn'] = actual_user
attrs['sn'] = lastname[1]
attrs['givenName']=lastname[0]
attrs['uid']=actual_uid
attrs['userPassword']=actual_pass
print (dn_1)
print (attrs)
l.add(dn_1,attributes=attrs)
print (l.result)
if __name__ == "__main__":
main(sys.argv[1:])
Result :{'result': 50, 'description': 'insufficientAccessRights', 'dn': '', 'message': 'no write access to parent', 'referrals': None, 'type': 'addResponse'}
Any help ?
It looks like you don't have the permission to create user accounts.
I ran some test code to check my theory where I bind to my LDAP server and try to create a user, the bind was successful but the Connection.add(...) failed with:
{'description': 'insufficientAccessRights', 'result': 50, 'type': 'addResponse', 'dn': '', 'message': '00000005: SecErr: DSID-03152857, problem 4003 (INSUFF_ACCESS_RIGHTS), data 0\n\x00', 'referrals': None}
Which to me looks very similar to your error.