I have a function I'm trying to execute which calls in 4 different variables:
def listdbtables(dbname, user, host, password):
try:
conn = psycopg2.connect("dbname = %s username = %s host = %s pass = %s" % dbname, % user, % host, % password)
curs = conn.cursor()
b = curs.execute("\l")
print b
except psycopg2.DatabaseError, ex:
print "I am unable to connect the database: " + ex
sys.exit(1)
I am unable to read in the variables with my current setup, how do I call in the variables properly in the conn variables.
Edit: Here is the error I am seeing:
File "./pg_meta.py", line 35
conn = psycopg2.connect("dbname = %s username = %s host = %s pass = %s" (% dbname, % user, % host, % password))
^
SyntaxError: invalid syntax
try this:
def listdbtables(dbname, user, host, password):
try:
conn = psycopg2.connect("dbname = %s username = %s host = %s pass = %s" % (dbname, user, host,password,))
curs = conn.cursor()
b = curs.execute("\l")
print b
except psycopg2.DatabaseError, ex:
print "I am unable to connect the database: " + ex
sys.exit(1)
the format portion needs to be a tuple for multiple values. you should also consider using String.format
I think this is wrong syntax:
conn = psycopg2.connect("dbname = %s username = %s host = %s pass = %s" % dbname, % user, % host, % password)
Change it to:
conn = psycopg2.connect("dbname = %s username = %s host = %s pass = %s" %(dbname, user, host, password))
another way it to use .format():
conn = psycopg2.connect("dbname = {0} username = {1} host = {2} pass = {3}".format(dbname, user, host, password))
Your error is saying that you have incorrect syntax in your program. When using the % operator with multiple variables Python requires you to use parenthesis. Change your code to this instead:
conn = psycopg2.connect("dbname = %s username = %s host = %s pass = %s" % (dbname, % user, % host, % password))
However, the recommend way to format variables into strings in newer Python versions would be to use the .format() method, instead of old style string formatting with %:
conn = psycopg2.connect("dbname = {} username = {} host = {} pass = {}".format(
dbname, user, host, password)
)
Both methods have their advantageous and disadvantageous, so I encourage you to find which one works best for you. A good website to compare the two methods is pyformat.info.
Related
I have coded a server with python ssl.socket and whenever i receive a message that is not SSL the server crashes with this following error :
Traceback (most recent call last):
File "./server.py", line 88, in
keyfile="server.key")
File "/usr/lib64/python2.7/ssl.py", line 936, in wrap_socket
ciphers=ciphers)
File "/usr/lib64/python2.7/ssl.py", line 611, in init
self.do_handshake()
File "/usr/lib64/python2.7/ssl.py", line 833, in do_handshake
self._sslobj.do_handshake()
ssl.SSLEOFError: EOF occurred in violation of protocol (_ssl.c:579)
Here is the server code:
#!/usr/bin/python
import socket
import ssl
import sys
import os
import MySQLdb
import logging
from function import encrypt
# Create a TCP/IP socket
bindsocket = socket.socket()
bindsocket.bind(('', 10023))
bindsocket.listen(5)
# Bind the socket to the port
print >>sys.stderr, 'starting up on port %s' % bindsocket.bind
def process_data(connstream, data):
data1 = data.rstrip()
data_list = data1.split(",")
print >>sys.stderr, 'received "%s"' % data_list
if data1:
print >>sys.stderr, 'Checking Database'
con = MySQLdb.connect('localhost', 'XXXX', 'XXXX', 'XXXX')
cursor = con.cursor()
cursor.execute("SELECT * FROM hostname WHERE systemname = '%s'" % data_list[1])
rowsz = cursor.rowcount
result = cursor.fetchall()
if data_list[0] == "server":
if rowsz == 1:
print >>sys.stderr, 'Updating Hostname:"%s"' % data_list[1]
cursor.execute("UPDATE hostname set systemname='%s',domain='%s',vendor='%s',model='%s',machinetype='%s',os='%s',osrelease='%s',machineserial='%s' where systemname = '%s'" % (data_list[1], data_list[2], data_list[3], data_list[4], data_list[5], data_list[6], data_list[7], data_list[8], data_list[1]))
cursor.execute("SELECT id FROM hostname where systemname = '%s'" % data_list[1])
detids = cursor.fetchone()
print >>sys.stderr, 'Updating datacenter for ID: "%s"' % detids[0]
cursor.execute("UPDATE details set datacenter='%s' where detid = '%s'" % (data_list[9], detids[0]))
elif rowsz < 1:
print >>sys.stderr, 'Inserting Hostname:"%s"' % data_list[1]
cursor.execute("INSERT INTO hostname (systemname, domain, vendor, model, machinetype, os, osrelease, machineserial) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (data_list[1], data_list[2], data_list[3], data_list[4], data_list[5], data_list[6], data_list[7], data_list[8]))
default= 'default'
defaultint = 1
cursor.execute("SELECT id FROM hostname where systemname = '%s'" % data_list[1])
detids = cursor.fetchone()
print >>sys.stderr, 'Updating details for id: "%s"' % detids[0]
cursor.execute("INSERT INTO details (detid, inservice, globalstatus, managedby, machineconsole, passwd, application, datacenter, rackspace, mlog) VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s')" % (detids[0], defaultint, defaultint, default, default, default, default, default, default, default))
else:
print >>sys.stderr, 'Nothing updated'
elif data_list[0] == "network":
cursor.execute("SELECT id FROM hostname where systemname = '%s'" % data_list[1])
detids = cursor.fetchone()
cursor.execute("SELECT * from network where netid = '%s'" % detids[0])
rowszz = cursor.rowcount
print >>sys.stderr, 'Count : "%s"' % rowszz
if rowszz >= 1:
print >>sys.stderr, 'Updating network details for id: "%s"' % detids[0]
for i in xrange(2, len(data_list), 5):
if data_list[i]:
sqlcmd = "UPDATE network set netid='{val1}', interface='{val2}', ipaddress='{val3}', macaddress='{val4}', gateway='{val5}', interfacestatus='{val6}' WHERE interface = '{val2}' AND netid = '{val1}'".format(val1=detids[0], val2=data_list[i], val3=data_list[i+1], val4=data_list[i+2], val5=data_list[i+3], val6=data_list[i+4])
cursor.execute(sqlcmd)
else:
print >>sys.stderr, 'Inserting network details for id: "%s"' % detids[0]
for i in xrange(2, len(data_list), 5):
if data_list[i]:
sqlcmd = "INSERT INTO network (netid, interface, ipaddress, macaddress, gateway, interfacestatus) VALUES ('{val1}', '{val2}', '{val3}', '{val4}', '{val5}', '{val6}')".format(val1=detids[0], val2=data_list[i], val3=data_list[i+1], val4=data_list[i+2], val5=data_list[i+3], val6=data_list[i+4])
cursor.execute(sqlcmd)
elif data_list[0] == "rootpwd":
print >>sys.stderr, 'Updating root pwd for :"%s"' % data_list[1]
cursor.execute("SELECT id FROM hostname where systemname = '%s'" % data_list[1])
detsids = cursor.fetchone()
ciphertext = encrypt(data_list[2])
cursor.execute("UPDATE details set passwd='%s' where detid = '%s'" % (ciphertext, detsids[0]))
else:
print >>sys.stderr, 'no more data from', client_address
return False
def deal_with_client(connstream):
break
data = connstream.read()
while True:
newsocket, fromaddr = bindsocket.accept()
connstream = ssl.wrap_socket(newsocket,
server_side=True,
certfile="server.crt",
keyfile="server.key")
try:
deal_with_client(connstream)
finally:
connstream.shutdown(socket.SHUT_RDWR)
connstream.close()
and the client is sending information to server like this :
/bin/echo $variable,$variable,$variable | /usr/bin/openssl s_client -quiet -connect hostname:port
its there a way to avoid server crash if the message it receives doesn't make sense for the server or if message received its wrong format ?
thank you.
I've written a script to decrypt old passwords and accounts that I can't access because I can't access my old email (again because I can't remember the passwords haha).
import os
import sqlite3
import win32crypt
import sys
try:
path = sys.argv[1]
except IndexError:
for w in os.walk(os.getenv('USERPROFILE')):
if 'Chrome' in w[1]:
path = str(w[0]) + '/Chrome/User Data/Default/Login Data'
try:
print ('[+] Opening ' + path)
conn = sqlite3.connect(path)
cursor = conn.cursor()
except Exception as e:
print ('[-] %s' % (e))
sys.exit(1)
# Get the results
try:
cursor.execute('SELECT action_url, username_value, password_value FROM logins')
except Exception as e:
print ('[-] %s' % (e))
sys.exit(1)
data = cursor.fetchall()
Everything is fine up to here.
for result in data:
try:
password = win32crypt.CryptUnprotectData(result[2], None)
except Exception as e:
print('[-] %s' % (e))
pass
if password:
print("[+] URL: {} Username: {} Password: {}".format(result[0], result[1], password))
else: print("Unable to extract data")
I get this error: (-2146893813, 'CryptProtectData', 'Key not valid for use in specified state.')
Thanks to gilliduck for pointing out my typo!
I believe it's
CryptUnprotectData
not
CryptUnprotectEDData
I have been trying to do some password cracking using L0phtcrack. I have a LDIF file that cannot be imported in to l0phtcrack as it is. lophtcrack does not also import from a csv file. The only option that I have using lophtcrack is to import from a Unix shadow file. Has anyone tried out converting a ldif file to a shadow file format in python ? If yes, would you mind sharing the script here.
Thanks
UPDATE - I made a few edits to code that i found online and got it to work. I now have a shadow file from my LDIF extract.
#!/usr/bin/env python
"""
Read in a ldap/ldif file and split out a unix-formated password file to run in
john the ripper for password auditing.
Typical usage (on the ldap server):
Dump the ldap database
slapcat > ldif.out
Convert it to passwd format
ldap-passwd-dump.py ldif.out passwd.out
Run john to crack passwords
john passwd.out
Aaron Peterson <aaron#midnightresearch.com>
"""
import sys, base64, re, os
class Results:
"""
Keep all user results here
"""
def __init__(self):
self.users = []
def addUser(self, user):
# Check for duplicates
dupe = 0
for u in self.users:
if u.uid == user.uid:
dupe = 1
break
if not dupe:
print " [*] Adding new user [%s, %s] to results" % (user.cn, user.uid)
self.users.append(user)
else:
print " [*] Not adding duplicate user [%s]" % user.cn
class User(list):
def __init__(self, hash=None, base64=None, password=None, cn=None, uid=None):
self.hash = hash
self.uid = uid
self.base64 = base64
self.password = password
self.cn = cn
list.__init__(self)
class LDIFCrack:
def main(self):
# Open file
f = open(self.ldif, "r")
# Load in the first user
user = User()
isInGroup=0
# Load lines into a "user"
for line in f:
if re.compile(r"^\s*$").search(line):
# Only append the old user if it's in the right group, and has a password set
if isInGroup and user.hash:
self.results.addUser(user)
# Reset user and counter
user = User()
isInGroup=0
# Make sure we test the right groups
if re.compile(self.groupMatch).search(line):
isInGroup=1
# Pull out the password
match = re.compile(r"userpassword: (.*)$").search(line)
if match:
user.hash = match.group(1)
# uid
match = re.compile(r"uid: (.*)$").search(line)
if match:
user.uid= match.group(1)
# Grab the common name
matchCn = re.compile(r"cn: (.*)$").search(line)
if matchCn:
user.cn = matchCn.group(1)
def printPasswd(self, file):
f = open(file, "w")
for user in self.results.users:
line = "%s:%s:::%s" % (user.uid, user.hash, user.cn)
f.write(line + "\n")
print " [*] %s" % line
f.close()
print " [*] Wrote [%s] password lines to [%s] " % (len(self.results.users), file)
def __init__(self, ldif, groupMatch):
self.ldif = ldif
self.results = Results()
self.groupMatch = groupMatch
self.main()
if __name__ == "__main__":
if len(sys.argv) < 3:
print "\nusage: %s <ldif file> <output password file> [<user matchString>]" % sys.argv[0]
print " example: %s ldif.out passwd.txt \"^ou: MyGroup\"" % sys.argv[0]
print " (matchString default is \"objectClass: posixAccount\")\n"
sys.exit(1)
ldif = sys.argv[1]
passwdFile = sys.argv[2]
if not os.path.exists(ldif):
print " [!] LDIF Input file [%s] does not exist..." % ldif
sys.exit(1)
if os.path.exists(passwdFile):
print " [!] Won't overwrite existing passwd file [%s]" % passwdFile
sys.exit(1)
# Will match the user against this group before cracking it if it's set
if len(sys.argv) == 4:
groupMatch = sys.argv[3]
else:
groupMatch = "objectClass: posixAccount"
ldifcrack = LDIFCrack(ldif, groupMatch)
ldifcrack.printPasswd(passwdFile)
print " [*] Done"
I'm a php guy and I don't understand python very well... so .. excuse me if my question is stupid.
I have 2 php scripts (client.php and worker.php) using Gearman, that I need to convert to python versions. I have been able to do it partially, but I'm stuck. First, here are my two scripts:
client.py
#!/usr/bin/env python
import gearman
import json
RBLS = [
'b.barracudacentral.org',
'bl.emailbasura.org'
]
IP = '1.2.3.4'
def check_request_status(job_request):
if job_request.complete:
print "Job %s finished! Result: %s - %s" % (job_request.job.unique, job_request.state, job_request.result)
elif job_request.timed_out:
print "Job %s timed out!" % job_request.unique
elif job_request.state == JOB_UNKNOWN:
print "Job %s connection failed!" % job_request.unique
data = {"ip": IP, "rbls": RBLS}
serialized_data = json.dumps(data)
gm_client = gearman.GearmanClient(['localhost:4730'])
completed_job_request = gm_client.submit_job("runcheck", serialized_data)
check_request_status(completed_job_request)
worker.py
#!/usr/bin/env python
import gearman
import sys
import socket
import re
import json
from dns.resolver import Resolver, NXDOMAIN, NoNameservers, Timeout, NoAnswer
from threading import Thread
# This hardcoded RBLS need to be passed by gearman client script
# RBLS = ['xbl.spamhaus.org', 'zen.spamhaus.org']
class Lookup(Thread):
def __init__(self, host, dnslist, listed, resolver):
Thread.__init__(self)
self.host = host
self.listed = listed
self.dnslist = dnslist
self.resolver = resolver
def run(self):
try:
host_record = self.resolver.query(self.host, "A")
if len(host_record) > 0:
self.listed[self.dnslist]['LISTED'] = True
self.listed[self.dnslist]['HOST'] = host_record[0].address
text_record = self.resolver.query(self.host, "TXT")
if len(text_record) > 0:
self.listed[self.dnslist]['TEXT'] = "\n".join(text_record[0].strings)
self.listed[self.dnslist]['ERROR'] = False
except NXDOMAIN:
self.listed[self.dnslist]['ERROR'] = True
self.listed[self.dnslist]['ERRORTYPE'] = NXDOMAIN
except NoNameservers:
self.listed[self.dnslist]['ERROR'] = True
self.listed[self.dnslist]['ERRORTYPE'] = NoNameservers
except Timeout:
self.listed[self.dnslist]['ERROR'] = True
self.listed[self.dnslist]['ERRORTYPE'] = Timeout
except NameError:
self.listed[self.dnslist]['ERROR'] = True
self.listed[self.dnslist]['ERRORTYPE'] = NameError
except NoAnswer:
self.listed[self.dnslist]['ERROR'] = True
self.listed[self.dnslist]['ERRORTYPE'] = NoAnswer
class RBLSearch(object):
def __init__(self, lookup_host):
self.lookup_host = lookup_host
self._listed = None
self.resolver = Resolver()
self.resolver.timeout = 0.2
self.resolver.lifetime = 1.0
def search(self):
if self._listed is not None:
pass
else:
host = self.lookup_host.split(".")
host = ".".join(list(reversed(host)))
self._listed = {'SEARCH_HOST': self.lookup_host}
threads = []
for LIST in RBLS:
self._listed[LIST] = {'LISTED': False}
query = Lookup("%s.%s" % (host, LIST), LIST, self._listed, self.resolver)
threads.append(query)
query.start()
for thread in threads:
thread.join()
return self._listed
listed = property(search)
def print_results(self):
listed = self.listed
print("")
print("--- DNSBL Report for %s ---" % listed['SEARCH_HOST'])
for key in listed:
if key == 'SEARCH_HOST':
continue
if not listed[key].get('ERROR'):
if listed[key]['LISTED']:
print("Results for %s: %s" % (key, listed[key]['LISTED']))
print(" + Host information: %s" % \
(listed[key]['HOST']))
if 'TEXT' in listed[key].keys():
print(" + Additional information: %s" % \
(listed[key]['TEXT']))
else:
#print "*** Error contacting %s ***" % key
pass
def task_listener_runcheck(gearman_worker, gearman_job):
jdata = json.loads(gearman_job.data)
host = jdata['ip']
ip = host
RBLS = jdata['rbls']
print("Looking up: %s (please wait)" % host)
pat = re.compile("\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")
is_ip_address = pat.match(host)
if not is_ip_address:
try:
ip = socket.gethostbyname(host)
print("Hostname %s resolved to ip %s" % (host,ip))
except socket.error:
print("Hostname %s can't be resolved" % host)
ip = ""
if ip:
searcher = RBLSearch(ip)
searcher.print_results()
return "RunCheck was successfull"
gm_worker = gearman.GearmanWorker(['localhost:4730'])
gm_worker.set_client_id('python-worker')
gm_worker.register_task('runcheck', task_listener_runcheck)
gm_worker.work()
Here is how this 2 scripts are working: client.py passes the IP address and array of rbl's to the worker.py. Then worker gets the IP address and check it against all the rbl's.
The problem is that I don't know how to use the RBLS inside the RBLSearch class. It's working if I hardcode the RBLS in the beginning of the script (See worker.py, Line 12), but it's not working if I define RBLS in task_listener_runcheck
I have been able to solve it. Here is the edited version (in case anyone want it):
worker.py
#!/usr/bin/env python
import gearman
import sys
import socket
import re
import json
from dns.resolver import Resolver, NXDOMAIN, NoNameservers, Timeout, NoAnswer
from threading import Thread
class Lookup(Thread):
def __init__(self, host, dnslist, listed, resolver):
Thread.__init__(self)
self.host = host
self.listed = listed
self.dnslist = dnslist
self.resolver = resolver
def run(self):
try:
host_record = self.resolver.query(self.host, "A")
if len(host_record) > 0:
self.listed[self.dnslist]['LISTED'] = True
self.listed[self.dnslist]['HOST'] = host_record[0].address
text_record = self.resolver.query(self.host, "TXT")
if len(text_record) > 0:
self.listed[self.dnslist]['TEXT'] = "\n".join(text_record[0].strings)
self.listed[self.dnslist]['ERROR'] = False
except NXDOMAIN:
self.listed[self.dnslist]['ERROR'] = False
self.listed[self.dnslist]['ERRORTYPE'] = NXDOMAIN
except NoNameservers:
self.listed[self.dnslist]['ERROR'] = True
self.listed[self.dnslist]['ERRORTYPE'] = NoNameservers
self.listed[self.dnslist]['TEXT'] = "%s - The operation timed out." % self.host
except Timeout:
self.listed[self.dnslist]['ERROR'] = True
self.listed[self.dnslist]['ERRORTYPE'] = Timeout
self.listed[self.dnslist]['TEXT'] = "%s - The operation timed out." % self.host
except NameError:
self.listed[self.dnslist]['ERROR'] = True
self.listed[self.dnslist]['ERRORTYPE'] = NameError
self.listed[self.dnslist]['TEXT'] = "%s - NameError" % self.host
except NoAnswer:
self.listed[self.dnslist]['ERROR'] = True
self.listed[self.dnslist]['ERRORTYPE'] = NoAnswer
self.listed[self.dnslist]['TEXT'] = "%s - The response did not contain an answer to the question." % self.host
class RBLSearch(object):
def __init__(self, lookup_host, rbls):
self.lookup_host = lookup_host
self.rbls = rbls
self._listed = None
self.resolver = Resolver()
self.resolver.timeout = 0.2
self.resolver.lifetime = 1.0
def search(self):
if self._listed is not None:
pass
else:
host = self.lookup_host.split(".")
host = ".".join(list(reversed(host)))
self._listed = {'SEARCH_HOST': self.lookup_host}
threads = []
for LIST in self.rbls:
self._listed[LIST] = {'LISTED': False}
query = Lookup("%s.%s" % (host, LIST), LIST, self._listed, self.resolver)
threads.append(query)
query.start()
for thread in threads:
thread.join()
return self._listed
listed = property(search)
def print_results(self):
listed = self.listed
print("")
print("--- DNSBL Report for %s ---" % listed['SEARCH_HOST'])
for key in listed:
if key == 'SEARCH_HOST':
continue
if not listed[key].get('ERROR'):
if listed[key]['LISTED']:
print("Results for %s: %s" % (key, listed[key]['LISTED']))
print(" + Host information: %s" % \
(listed[key]['HOST']))
if 'TEXT' in listed[key].keys():
print(" + Additional information: %s" % \
(listed[key]['TEXT']))
else:
print("Not listed in %s" % (key))
else:
#print "*** Error contacting %s ***" % key
pass
def task_listener_runcheck(gearman_worker, gearman_job):
jdata = json.loads(gearman_job.data)
host = jdata['ip']
rbls = jdata['rbls']
ip = host
print("Looking up: %s (please wait)" % host)
pat = re.compile("\d{1,3}.\d{1,3}.\d{1,3}.\d{1,3}")
is_ip_address = pat.match(host)
if not is_ip_address:
try:
ip = socket.gethostbyname(host)
print("Hostname %s resolved to ip %s" % (host,ip))
except socket.error:
print("Hostname %s can't be resolved" % host)
ip = ""
if ip:
searcher = RBLSearch(ip, rbls)
searcher.print_results()
return "RunCheck was successfull"
gm_worker = gearman.GearmanWorker(['localhost:4730'])
gm_worker.set_client_id('python-worker')
gm_worker.register_task('runcheck', task_listener_runcheck)
gm_worker.work()
I am using mysqldb/python to push some data into a mysql db.
The script parses a bunch of XML files for the data.
The MySQL server seems to quit and give me a '#2002 - The server is not responding (or the local MySQL server's socket is not correctly configured)' error midway through the transactions - in a different place every time I run it (so I am assuming its not a specific piece of data that is making it fall over...)
It works perfectly until it reaches ~12 or 13 file and it gives me this error:
Error 2003: Can't connect to MySQL server on 'localhost' (10055)
Traceback (most recent call last):
File "sigFileParser.py", line 113, in <module>
doParser(sigfile_filename)
File "sigFileParser.py", line 106, in
doParser
doFormatsPush(packedFormats)
File "sigFileParser.py", line 27, in
doFormatsPush
sys.exit (1)
NameError: global name 'sys' is not defined
Once the error occurs I can not get into MySQL console or via PHOPmyadmin
If I leave if for a while, I can get back into MySQL
MySQL tables:
CREATE TABLE IF NOT EXISTS patterns
(Version int(3),
DateCreated DATETIME,
SigID int(4),
SigSpecificity CHAR(10),
ByteSeqReference CHAR(12),
MinFragLength int(4),
Position int(4),
SubSeqMaxOffset int(4),
SubSeqMinOffset int(4),
Pattern TEXT)
and
CREATE TABLE IF NOT EXISTS formats
(Version int(3),
DateCreated DATETIME,
FormatID int(4),
FormatName TEXT,
PUID TEXT,
FormatVersion TEXT,
FormatMIMEType TEXT,
InternalSignatureID int(4),
Extension TEXT,
HasPriorityOverFileFormatID int(4))
Py code
from lxml import etree
import re, os, MySQLdb
def doPatternPush(packedPatterns):
try:
db = MySQLdb.connect (host = "localhost", user = "root", passwd = "", db = "sigfiles")
c = db.cursor()
c.execute('''INSERT INTO sigfiles.patterns
(Version,DateCreated,SigID,SigSpecificity,ByteSeqReference,MinFragLength,Position,SubSeqMaxOffset,SubSeqMinOffset,Pattern)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''', packedPatterns)
db.commit()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
return (db)
def doFormatsPush(packedFormats):
try:
db = MySQLdb.connect (host = "localhost", user = "root", passwd = "", db = "sigfiles")
c = db.cursor()
c.execute('''INSERT INTO sigfiles.formats
(Version,DateCreated,FormatID,FormatName,PUID,FormatVersion,FormatMIMEType,InternalSignatureID,Extension,HasPriorityOverFileFormatID)
VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s)''', packedFormats)
db.commit()
except MySQLdb.Error, e:
print "Error %d: %s" % (e.args[0], e.args[1])
sys.exit (1)
return(db)
def doParser(sigfile_filename):
tree = etree.parse(sigfile_filename)
root = tree.getroot()
attributes = root.attrib
if 'DateCreated' in root.attrib:
DateCreated = (attributes["DateCreated"])
if 'Version' in root.attrib:
Version = (attributes["Version"])
##--------- get internal sig details ------------------
for a in range (len(root[0])): #loops for sig ID
attributes = root[0][a].attrib
SigID=(attributes["ID"])
SigSpecificity = (attributes["Specificity"])
for b in range (len(root[0][a])): # loops for sequence pattern inside each sig
attributes = root[0][a][b].attrib
if 'Reference' in root[0][a][b].attrib:
ByteSeqReference = (attributes["Reference"])
else:
ByteSeqReference = "NULL"
attributes = root[0][a][b][0].attrib
if 'MinFragLength' in root[0][a][b][0].attrib:
MinFragLength=(attributes["MinFragLength"])
else:
MinFragLength=''
if 'Position' in root[0][a].attrib:
Position=(attributes["Position"])
else:
Position=''
if 'SubSeqMaxOffset' in root[0][a][b][0].attrib:
SubSeqMaxOffset=(attributes["SubSeqMaxOffset"])
else:
SubSeqMaxOffsee = ''
if 'SubSeqMinOffset' in root[0][a][b][0].attrib:
SubSeqMinOffset=(attributes["SubSeqMinOffset"])
else:
SubSeqMinOffset = ''
Pattern = root[0][a][b][0][0].text
packedPatterns = [Version,DateCreated,SigID,SigSpecificity,ByteSeqReference,MinFragLength,Position,SubSeqMaxOffset,SubSeqMinOffset,Pattern]
doPatternPush(packedPatterns)
##-------- get format ID details-------------
for a in range (len(root[1])):
attributes = root[1][a].attrib
if 'ID' in root[1][a].attrib:
FormatID = (attributes['ID'])
else:
FormatID = "NULL"
if 'Name' in root[1][a].attrib:
FormatName = (attributes['Name'])
else:
FormatName = "NULL"
if 'PUID' in root[1][a].attrib:
PUID = (attributes['PUID'])
else:
PUID = "NULL"
if 'Version' in root[1][a].attrib:
FormatVersion = (attributes['Version'])
else:
FormatVersion = "NULL"
if 'MIMEType' in root[1][a].attrib:
FormatMIMEType = (attributes['MIMEType'])
else:
FormatMIMEType = "NULL"
InternalSignatureID,Extension,HasPriorityOverFileFormatID = ('', 'NULL', '')
for b in range (len(root[1][a])): #extracts the tags for each format ID
tagType = root[1][a][b].tag
tagText = root[1][a][b].text
tagType = re.sub('{http://www.nationalarchives.gov.uk/pronom/SignatureFile}', '', tagType)
if tagType == 'InternalSignatureID':
InternalSignatureID = tagText
elif tagType == 'Extension':
Extension = tagText
HasPriorityOverFileFormatID = ''
else:
HasPriorityOverFileFormatID = tagText
Extension = 'NULL'
packedFormats = [Version,DateCreated,FormatID,FormatName,PUID,FormatVersion,FormatMIMEType,InternalSignatureID,Extension,HasPriorityOverFileFormatID]
doFormatsPush(packedFormats)
if __name__ == "__main__":
path = "C:\Users\NDHA\Desktop\droid sigs all"
for (path, dirs, files) in os.walk(path):
for file in files:
sigfile_filename = str(path)+"\\"+str(file)
doParser(sigfile_filename)
print sigfile_filename
db.close()
All the XML comes from here: http://www.nationalarchives.gov.uk/aboutapps/pronom/droid-signature-files.htm
The error you get tells you exactly what's wrong
NameError: global name 'sys' is not defined
You don't import sys in your python file.
As for the db connection, if your socket is not placed in /tmp/mysql.sock, you can specify where to look for it when you try to connect to the db using the unix_socket parameter.
Try:
db = MySQLdb.connect (unix_socket = 'path_to_sock', host = "localhost",
user = "root", passwd = "", db = "sigfiles")
Where you replace 'path_to_sock' with the actual path to the mysql sock.
Other stuff you should check in case that isn't the issue:
Check to make sure the username/password combination is correct
Try stopping and re-starting the mysqld service
Check the error log files for more specific errors
This is your first error:
Error 2003: Can't connect to MySQL server on 'localhost' (10055)
It seems you disconnect from MySQL at some point. Check your code and see if you're explicitly or implicitly getting disconnected from the server and also check if your MySQL server is still listening to connections... maybe you're killing the server from outside your app... who knows? :)