Can't connect to MySQL server on host - python

I have am using pYMYSQL to connect db and used buildozer to deploy on android device.
Can't connect to MySQL server on 'freedb.tech'
ERROR
05-14 17:15:20.090 4020 4058 org.test.myapp I python Something went
wrong: (2003, "Can't connect to MySQL server on 'freedb.tech' ([Errno
7] No address associated with hostname)")
python file
from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.animation import Animation
from hoverable import HoverBehavior
from kivy.uix.image import Image
from kivy.uix.behaviors import ButtonBehavior
import json, glob
from datetime import datetime
from pathlib import Path
import random
import pymysql.cursors
db_string = "postgresql://[user]:[password]#ec2-54-197-100-79.compute-1.amazonaws.com:5432/d4bsdrtg9i2j5d"
Builder.load_file('design.kv')
class LoginScreen(Screen):
def sign_up(self):
self.manager.current = "sign_up_screen"
def login(self, uname, pword):
with open("users.json") as file:
users = json.load(file)
if uname in users and users[uname]['password'] == pword:
self.manager.current = 'login_screeen_success'
else:
anim = Animation(color=(0.6, 0.7, 0.1, 1))
anim.start(self.ids.login_wrong)
self.ids.login_wrong.text = "Wrong username or password!"
class RootWidget(ScreenManager):
pass
class SignUpScreen(Screen):
def add_user(self, uname, pword):
with open("users.json") as file:
users = json.load(file)
users[uname] = {'username': uname, 'password': pword,
'created': datetime.now().strftime("%Y-%m-%d %H-%M-%S")}
with open("users.json", 'w') as file:
json.dump(users, file)
self.manager.current = "sign_up_screen_success"
class SignUpScreenSuccess(Screen):
def go_to_login(self):
self.manager.transition.direction = 'right'
self.manager.current = "login_screen"
class LoginScreenSuccess(Screen):
def log_out(self):
self.manager.transition.direction = "right"
self.manager.current = "login_screen"
def get_quote(self, feel):
feel = feel.lower()
available_feelings = glob.glob("quotes/*txt")
available_feelings = [Path(filename).stem for filename in
available_feelings]
if feel in available_feelings:
with open(f"quotes/{feel}.txt", encoding='utf8') as file:
quotes = file.readlines()
self.ids.quote.text = random.choice(quotes)
else:
self.ids.quote.text = "Try another feeling"
def feed_back(self):
self.manager.current = "feedback_screen"
class ImageButton(ButtonBehavior, HoverBehavior, Image):
pass
class FeedbackScreen(Screen):
def send_feed(self, feed):
print(feed)
# connection = psycopg2.connect(user="[user]",
# password="[password]",
# host="ec2-54-197-100-79.compute-1.amazonaws.com",
# port="5432",
# database="d4bsdrtg9i2j5d")
# db = create_engine(db_string)
#
# postgres_insert_query = """INSERT INTO feed(feed_info) VALUES (%s)"""
#
# db.execute(postgres_insert_query, feed)
# print("Record inserted successfully into feed table")
try:
mydb = pymysql.connect(
host="freedb.tech",
user="freedbtech_reds",
password="Raskol#786",
database="freedbtech_redsdb"
)
mycursor = mydb.cursor()
sql = "INSERT INTO feed (feed_info) VALUES (%s)"
val = (feed,)
mycursor.execute(sql, val)
mydb.commit()
print(mycursor.rowcount, "Record inserted successfully into feed table")
self.manager.current = 'login_screen'
except pymysql.Error as err:
print("Something went wrong: {}".format(err))
class MainApp(App):
def build(self):
return RootWidget()
if __name__ == "__main__":
MainApp().run()
---------------------------------
buildozer spec
--------------------------------
requirements = python3,kivy,PyMySQL

your saying that these credentials are correct but your still can't connect to database
this is simple problem
Note:-
if you don't have internet access you get this error also
so to get internet access You have to add android.permissions = INTERNET in your buildozer.spec file
Happy coding (^_^)

Related

Python simple DNS resolver: memory leak

This code is a DNS resolver that check from a DB for an entry not older than 5 minutes.
#!/usr/bin/python3
from MySQLdb import _mysql as MySQL
from dnslib import RR, QTYPE, RCODE, A
from dnslib.label import DNSLabel
from dnslib.server import DNSServer, BaseResolver
from time import sleep, time
class MariaResolver(BaseResolver):
DELTA = 300
def __init__(self):
self.password = "********************"
def resolve(self, request, handler):
reply = request.reply()
qname = request.q.qname
fqdn = str(request.q.qname)
try:
if fqdn.find("iut-") == -1:
reply.header.rcode = RCODE.REFUSED
else:
hostname = fqdn.split(".")[0]
timestamp = int(time()) - self.DELTA
query = "SELECT ip FROM dns WHERE record='{}' AND timestamp>{}"
db = MySQL.connect("localhost", "dns", self.password, "salles")
db.query(query.format(hostname, timestamp))
result = db.store_result()
row = result.fetch_row(how=1)
if row:
ip = row[0]["ip"].decode("utf-8")
reply.add_answer(RR(qname, QTYPE.A, ttl=0,
rdata=A(ip)))
else:
reply.header.rcode = RCODE.REFUSED
db.close()
except Exception as e:
print(e)
reply.header.rcode = RCODE.REFUSED
return reply
if __name__ == '__main__':
resolver = MariaResolver()
udp_server = DNSServer(resolver, port=53)
udp_server.start_thread()
while udp_server.isAlive():
sleep(0.1)
This code leaks over time and I do not understand why.
In the Proxmox screenshot, you can see service restarted at the and.

Pyqt5- QMessageBox is appearing blank & program getting crashed

I am trying to run a code to load dataframe into a sql server using PyQT5 and also at the same time showing a "Please wait" pop up while data is being loaded in DB. I am utilizing QThread for the same. If there is some error then QMessageBox shows the error to the user. But my program is crashing with Message Box appearing blank. I am attaching both code and error snippet.Error Snippet
from functools import partial
from PyQt5 import QtCore, QtGui, QtWidgets
import configparser
import pyodbc
def ConfigSection(section,Config):
dict1 = {}
options = Config.options(section)
for option in options:
try:
dict1[option] = Config.get(section,option)
except:
dict1[option] = None
return dict1
class DatabaseWorker(QtCore.QObject):
started = QtCore.pyqtSignal()
finished = QtCore.pyqtSignal()
message = QtCore.pyqtSignal(object)
msg = ""
#QtCore.pyqtSlot()
def writeToDatabase(self, final_pivot):
self.started.emit()
Config = configparser.ConfigParser(interpolation=None)
try:
config_file = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'config.ini')
Config.read_string(open(config_file).read())
config_values = ConfigSection("Global", Config)
driver = config_values.get('driver')
server = config_values.get('server')
database = config_values.get('database')
username = config_values.get('username')
password = config_values.get('password')
con_str = ("Driver={" + driver + "};"
"Server=" + server + ";"
"Database=" + database + ";"
"UID=" + username + ";"
"PWD=" + password + ";")
cnxn = pyodbc.connect(con_str)
cursor = cnxn.cursor()
for i, row in final_pivot.iterrows():
sql = "INSERT INTO dbo.[FPP_Cleansed_MarketData] (ID,Region,Geography,Category,Company,Year,Adjusted_Sales,Sales_Units,Unit_Type) VALUES (" + "?," * (
len(row) - 1) + "?)"
cursor.execute(sql, tuple(row))
cnxn.commit()
except pyodbc.Error as ex:
sqlstate = ex.args[0]
if sqlstate == '28000':
msg = "Error: Error while logging in. Please check login credentials"
self.message.emit(msg)
credentials")
return -1
elif sqlstate == '23000':
msg = "Error: Trying to insert duplicate ID"
self.message.emit(msg)
return -1
else:
msg = "Error: Error encountered while inserting records into Data Warehouse"
self.message.emit(msg)
return -1
finally:
self.finished.emit()
class Windows_GUI(QtWidgets.QMainWindow):
def __init__(self, df):
QtWidgets.QMainWindow.__init__(self)
self.__threads = []
self.thread = QtCore.QThread(parent=self)
self.__threads.append(self.thread)
self.thread.start()
self.m_database_worker = DatabaseWorker()
self.m_database_worker.moveToThread(self.thread)
self.m_database_worker.started.connect(self.start_animation)
self.m_database_worker.message.connect(self.show_message)
wrapper = partial(self.m_database_worker.writeToDatabase,df)
QtCore.QTimer.singleShot(0, wrapper)
#QtCore.pyqtSlot()
def start_animation(self):
gif_path = "loading.gif"
self.loading_window = QtWidgets.QDialog()
self.loading_window.setWindowFlags(QtCore.Qt.SplashScreen)
self.loading_window.setAttribute(QtCore.Qt.WA_DeleteOnClose)
movie = QtGui.QMovie(gif_path, cacheMode=QtGui.QMovie.CacheAll)
movie_label = QtWidgets.QLabel(alignment=QtCore.Qt.AlignCenter)
movie_label.setFixedSize(100, 75)
movie_label.setMovie(movie)
text_label = QtWidgets.QLabel('Data is getting loaded into Data Warehouse. Please wait...')
text_label.setFont(QtGui.QFont('Arial',11))
vbox = QtWidgets.QHBoxLayout(self.loading_window)
vbox.addWidget(movie_label)
vbox.addWidget(text_label)
self.m_database_worker.finished.connect(self.close_window)
movie.start()
self.setVisible(False)
self.loading_window.show()
def show_message(self,msg):
try:
self.msg_box = QtWidgets.QMessageBox()
self.msg_box.setWindowTitle('Error')
self.msg_box.setIcon(QtWidgets.QMessageBox.Critical)
self.msg_box.information(self, 'Message', msg)
#self.msg_box.setText("Hello Rock the Brahma Bull")
self.msg_box.move(self.frameGeometry().center())
self.msg_box.exec_()
except Exception as e:
print(e)
def close_window(self):
for thread in self.__threads:
thread.quit()
thread.wait()
self.close()
if __name__ == "__main__":
import sys
import pandas as pd
app = QtWidgets.QApplication(sys.argv)
df = pd.read_excel('test.xlsx')
df['ID'] = ['Project_id_' + str(i) for i in range(len(df))]
df = df[['ID', 'Regions', 'Geographies', 'Categories', 'Companies', 'Year', 'Sales', 'Sales_Unit','Currency Conversion']]
w = Windows_GUI(df)
w.show()
sys.exit(app.exec_())

How to use python loop to through file and execute queries using parameter from text file

I'm trying to get a script working that takes each line from a file and use the line as input to run the SQL query. Specifically I'm trying to use a file that has a list of domains and use those domains names to query a postgresql database. Any help would be greatly appreciated!
from __future__ import print_function
try:
import psycopg2
except ImportError:
raise ImportError('\n\033[33mpsycopg2 library missing. pip install psycopg2\033[1;m\n')
sys.exit(1)
import re
import sys
import json
DB_HOST = 'crt.sh'
DB_NAME = 'certwatch'
DB_USER = 'guest'
def connect_to_db(domain_name):
try:
conn = psycopg2.connect("dbname={0} user={1} host={2}".format(DB_NAME, DB_USER, DB_HOST))
cursor = conn.cursor()
cursor.execute("SELECT ci.NAME_VALUE NAME_VALUE FROM certificate_identity ci WHERE ci.NAME_TYPE = 'emailAddress' AND reverse(lower(ci.NAME_VALUE)) LIKE reverse(lower('%{}'));".format(domain_name))
except:
print("\n\033[1;31m[!] Unable to connect to the database\n\033[1;m")
return cursor
def get_unique_emails(cursor, domain_name):
unique_emails = []
for result in cursor.fetchall():
matches=re.findall(r"\'(.+?)\'",str(result))
for email in matches:
#print(email)
if email not in unique_emails:
if "{}".format(domain_name) in email:
unique_emails.append(email)
return unique_emails
def print_unique_emails(unique_emails):
print("\033[1;32m[+] Total unique emails found: {}\033[1;m".format(len(unique_emails)))
for unique_email in sorted(unique_emails):
print(unique_email)
def write_unique_emails(unique_emails):
with open('unique_emails.json', 'w') as outfile:
json.dump(unique_emails, outfile, sort_keys=True, indent=4)
def get_domain_name():
filepath = 'file.txt'
with open(filepath) as fp:
for cnt, line in enumerate(fp):
print("Line {}: {}".format(cnt, line))
return line
if __name__ == '__main__':
domain_name = get_domain_name()
cursor = connect_to_db(domain_name)
unique_emails = get_unique_emails(cursor, domain_name)
print_unique_emails(unique_emails)
write_unique_emails(unique_emails)
Code below using sys.argv
from __future__ import print_function
try:
import psycopg2
except ImportError:
raise ImportError('\n\033[33mpsycopg2 library missing. pip install psycopg2\033[1;m\n')
sys.exit(1)
import re
import sys
import json
DB_HOST = 'crt.sh'
DB_NAME = 'certwatch'
DB_USER = 'guest'
def connect_to_db(domain_name):
try:
conn = psycopg2.connect("dbname={0} user={1} host={2}".format(DB_NAME, DB_USER, DB_HOST))
cursor = conn.cursor()
cursor.execute("SELECT ci.NAME_VALUE NAME_VALUE FROM certificate_identity ci WHERE ci.NAME_TYPE = 'emailAddress' AND reverse(lower(ci.NAME_VALUE)) LIKE reverse(lower('%{}'));".format(domain_name))
cursor.execute("SELECT ci.NAME_VALUE NAME_VALUE FROM certificate_identity ci WHERE ci.NAME_TYPE = 'serialNumber' AND reverse(lower(ci.NAME_VALUE)) LIKE reverse(lower('%{}'));".format(domain_name))
except:
print("\n\033[1;31m[!] Unable to connect to the database\n\033[1;m")
return cursor
def get_unique_emails(cursor, domain_name):
unique_emails = []
for result in cursor.fetchall():
matches=re.findall(r"\'(.+?)\'",str(result))
for email in matches:
#print(email)
if email not in unique_emails:
if "{}".format(domain_name) in email:
unique_emails.append(email)
return unique_emails
def get_unique_serialNumber(cursor, domains):
unique_domains = []
for result in cursor.fetchall():
matches=re.findall(r"\'(.+?)\'",str(result))
for serialNumber in matches:
if serialNumber not in unique_serialNumber:
if ".{}".format(domain_name) in serialNumber:
unique_serialNumber.append(serialNumber)
return unique_serialNumber
def print_unique_serialNumber(unique_serialNumber):
for unique_serialNumber in sorted(unique_serialNumber):
print(unique_serialNumber)
def print_unique_emails(unique_emails):
print("\033[1;32m[+] Total unique emails found: {}\033[1;m".format(len(unique_emails)))
for unique_email in sorted(unique_emails):
print(unique_email)
def write_unique_emails(unique_emails):
with open('read.json', 'w') as outfile:
json.dump(unique_emails, outfile, sort_keys=True, indent=4)
def get_domain_name():
if len(sys.argv) <= 1:
print("\n\033[33mUsage: python emails_from_ct_logs.py <target_domain>\033[1;m\n")
sys.exit(1)
else:
return sys.argv[1]
if __name__ == '__main__':
domain_name = get_domain_name()
cursor = connect_to_db(domain_name)
unique_emails = get_unique_emails(cursor, domain_name)
print_unique_emails(unique_emails)
write_unique_emails(unique_emails)
unique_serialNumber = get_unique_serialNumber(cursor, domain_name)
print_unique_serialNumber(unique_serialNumber)
Check out Psycopg2. Without knowing all the details of your db it's going to be impossible to do a "cut & paste" code dump. The basics are covered here, which hopefully is enough to get you going. When or if you have more specific questions create a new thread.

Parse json from mysql in flask to get a field from record

Mind that I am new to flask and python for that matter, I appreciate any help that anyone gives. I'm looking to access one of the fields of my JSON response(just the field not the entire response), how should I go about parsing the response. Image of the response attached below,thanks.
This is my main thread
from flask import Flask,render_template,request
from Qhandler import Qhandler
from MakePlayer import MakePlayer
app = Flask(__name__)
#app.route('/createplayer',methods=['GET','POST'] )
def showCreatePlayer():
if request.method == 'POST':
MakePlayer(request.form['playername'],request.form['playerteam'],request.form['playerrole'], request.form['playerpos'])
return "created player: <br>"+request.form['playername']+" "+request.form['playerteam']+" "+request.form['playerrole']+" "+request.form['playerpos']
return render_template("createPlayer.html")
#app.route('/sucess')
def success():
return "success"
#app.route('/showplayers')
def showPlayers():
Q = Qhandler()
return Q.displayQuery(""" select * from Player""")
if __name__ == '__main__':
app.run(debug=True)
This is my query handler
from flask import Flask, jsonify, json
from flaskext.mysql import MySQL
class Qhandler(object):
#global mysql
global cursor
global connection
global mysql
# database connection
app = Flask(__name__)
mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = 'root'
app.config['MYSQL_DATABASE_DB'] = 'Optimizer'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)
def ins(self,query):
try:
connection=mysql.connect()
cursor = connection.cursor()
cursor.execute(query)
connection.commit()
except:
print "error running query"
finally:
#cursor.close()
connection.close()
def displayQuery(self,query):
try:
connection = mysql.connect()
cursor = connection.cursor()
cursor.execute(query)
fetchedData = cursor.fetchall()
fetchedData = jsonify(fetchedData)
#fetchedData = json.dumps(fetchedData)
#record = json.loads(fetchedData)
#print "the resonse is here:"
return fetchedData
except:
print "error running query"
finally:
#cursor.close()
connection.close()
current response is
screenshot of results
Use "fetchedData = json.dumps(fetchedData)" instead of "fetchedData = jsonify(fetchedData)" then create a json decoder and parse the response, refer to below :
def displayQuery(self,query):
try:
connection = mysql.connect()
cursor = connection.cursor()
cursor.execute(query)
fetchedData = cursor.fetchall()
fetchedData = json.dumps(fetchedData)
#create a json decoder
d = json.JSONDecoder()
fieldPlayerName = d.decode(fetchedData)
#parse the json that is returned ( fieldPlayerName[0][1])
print "should print the field with the player name",fieldPlayerName[0][1]
return fieldPlayerName[0][1]

Using attributes of an object in python

I have one class, which is reading data from a JSON file:
import os
import json
from db_connection import DB_Connection
class RA_Admin:
def __init__(self):
data = None
self.load_access_data()
def load_access_data(self):
with open('../docs/db_data.json') as data_file:
self.data = json.load(data_file)
a = RA_Admin()
db_con = DB_Connection(a.data)
db_con.read_data()
I wrote a second class to connect to a database:
import mysql.connector
class DB_Connection:
def __init__(self, data):
database_connection = None
cursor = None
user = data["database"]["user"]
paw = data["database"]["paw"]
ip_address = data["database"]["ip_adress"]
db_name = data["database"]["database_name"]
port = data["database"]["port"]
def read_data(self):
database_connection = mysql.connector.connect(user = self.user, password = self.paw, host=self.ip_address, port=self.port, database=self.db_name)
self.cursor = database_connection.cursor(dictionary=True)
I get the following error:
database_connection = mysql.connector.connect(user = self.user, password = self.paw, host=self.ip_address, port=self.port, database=self.db_name)
AttributeError: DB_Connection instance has no attribute 'user'
I can print the user in the __init__ method and the other attributes, but why are they not used in read_data?
You need self:
self.user = data["database"]["user"]
self.paw = data["database"]["paw"]
....
what-is-the-purpose-of-self-in-python

Categories

Resources