0

I would like to extract an ip address from mysql using python.

here is my code,

cnx = mysql.connector.connect(host=mysql_localhost, user=user, password=password, database=database)
cursor = cnx.cursor()
cursor.execute("select inet_ntoa(server) as ip from settings" )
#print 'Data inserted into Database'
results = cursor.fetchone()
print(results)
cursor.close()
cnx.close()

and my result is

('192.168.0.15',)

how can i save the result above to an ip address?

4
  • Can you elaborate on what you mean with "how can i save the result above to an ip address?". It's unclear what you actually want to do. Commented Jul 19, 2013 at 13:28
  • do you mean having the 192.168.0.15 value instead of ('192.168.0.15',)?
    – psukys
    Commented Jul 19, 2013 at 13:28
  • Yes, and also be able to use this value to connect to a server
    – Ossama
    Commented Jul 19, 2013 at 13:32
  • I would like to save this ip address to a variable called mysql_localhost and connect to a database using cnx = mysql.connector.connect(host=mysql_localhost, user=user, password=password, database=database)
    – Ossama
    Commented Jul 19, 2013 at 13:34

2 Answers 2

0

After doing

results = cursor.fetchone()

results has the value ('192.168.0.15',). That is a tuple consisting of one element, a string.

You get it with results[0].

If you want to connect to that host, simply do

mysql_target_host = results[0]

and then

cnx = mysql.connector.connect(host=mysql_target_host, user=user, password=password, database=database)

as I strictly vote against mysql_localhost because that could lead to confusion.

0

That is a human readable interpretation of an IP address?

You could use IPy to parse it further.

Im not sure what else you are after

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.