Hi there! I new learner in Python. And just couldn't get threading. I wrote this script.

import sys
import MySQLdb


if len(sys.argv)!=3:
    """the program name, mac and ip makes three"""
    """stop program and send error"""
    sys.exit("Must provide mac and ip!")

mac = sys.argv[1]
ip = sys.argv[2]

print 'Connected to server'

#saving ip to database!
# Open database connection
db = MySQLdb.connect("localhost","user","pass","dbname" )

# prepare a cursor object using cursor() method
cursor = db.cursor()

# Prepare SQL query to UPDATE required records
sql = "UPDATE mac_list SET ip ='"+ip+"' WHERE mac='"+mac+"'"
try:
   # Execute the SQL command
   cursor.execute(sql)
   # Commit your changes in the database
   db.commit()
   print (mac+' and '+ip+' entered to database')
except:
   # Rollback in case there is any error
   db.rollback()
   print ('something went wrong')
# disconnect from server
db.close()

And I want to thread it. So that when called it won't have to wait for the previous call. How can I do that?

Criticisms on above code will be greatly appreciated!
Thanks in advance!

P.S. An example call would be

python getmacip.py [some-MAC] [some-IP]

Recommended Answers

All 4 Replies

Yeah... I can see that... I would have rather gotten a quick answer than to read documentation... But thanks... I think... But if anyone else could help that will still be great!

I wrote a tutorial about threading. A simple one :).
Check out the tutorial section.

I would have rather gotten a quick answer than to read documentation...

The classic Lazy. :icon_rolleyes:
Though I will admit, reading documentation isn't a very good
tutorial source and it is quite boring.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.