You are not logged in.
Dear visitor, welcome to Aqua Computer Forum. If this is your first visit here, please read the Help. It explains how this page works. You must be registered before you can use all the page's features. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.
Quoted from "messi@planung"
hi,
ich mache (lerne) gerade dasselbe und dieses tutorial kann ich da nur weiter empfehlen !
edit: schau dir vorallem seite 3 des tutorials an, ist wirklich super einfach
gruß,
messi
![]() |
Source code |
1 2 3 4 5 |
from time import ctime ..... ..... ..... print ctime() |
![]() |
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
#!/usr/bin/env python # MySQL-Connector laden; 3rd-party import MySQLdb # Verbindung herstellen _connection = MySQLdb.connect( host='your_hostname', user='your_username', passwd='your_password', db='your_database') # Cursor beziehen; in diesem Fall einen, der den Zugriff # per Feldname und nicht nur per Index ermöglicht cursor = _connection.cursor(MySQLdb.cursors.DictCursor) # SQL-Query absetzen cursor.execute(''' SELECT vorname, nachname FROM `personen` WHERE (alter > 18) ORDER BY nachname DESC; ''') # Alle Ergebniszeilen holen rows = cursor.fetchall() # Ergebnisse ausgeben print '%d Personen' % len(rows) print '-' * 12 for row in rows: print '%s, %s' % (row['nachname'], row['vorname']) |
![]() |
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
#!/usr/bin/python import MySQLdb import os import time db = MySQLdb.connect(host="localhost", user="apache", passwd="apache",db="apache") cursor = db.cursor() cursor.execute("SELECT COUNT(*) FROM access_log") row = cursor.fetchone() anz_log = row[0] print anz_log cursor.execute("DELETE FROM access_log WHERE agent='-'") cursor.execute("DELETE FROM access_log WHERE remote_host='192.168.0.1'") cursor.execute("SELECT COUNT(*) FROM access_log") row = cursor.fetchone() new_anz_log = row[0] print new_anz_log zeit = time.time() if anz_log>50: sql_create_statement = "create table access_log_"+str(zeit)[0:-3]+""" ( id char(19) , agent varchar(255) , bytes_sent int unsigned , child_pid smallint unsigned, cookie varchar(255), machine_id varchar(25), request_file varchar(255), referer varchar(255) , remote_host varchar(50) , remote_logname varchar(50) , remote_user varchar(50) , request_duration smallint unsigned , request_line varchar(255), request_method varchar(10) , request_protocol varchar(10) , request_time char(28), request_uri varchar(255), request_args varchar(255), server_port smallint unsigned, ssl_cipher varchar(25), ssl_keysize smallint unsigned, ssl_maxkeysize smallint unsigned, status smallint unsigned , time_stamp int unsigned , virtual_host varchar(255) ); """ print sql_create_statement cursor.execute(sql_create_statement) cursor.execute("SELECT * FROM access_log WHERE 1") rows = cursor.fetchall() for row in rows: sqlstatement = 'INSERT INTO access_log_'+str(zeit)[0:-3]+' VALUES(\'%s\',\'%s\'\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%s\',\'%$ print sqlstatement cursor.execute(sqlstatement) |
![]() |
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
server ~ # python ./python_test.py build 68 66 create table access_log_1148226196 ( id char(19) , agent varchar(255) , bytes_sent int unsigned , child_pid smallint unsigned, cookie varchar(255), machine_id varchar(25), request_file varchar(255), referer varchar(255) , remote_host varchar(50) , remote_logname varchar(50) , remote_user varchar(50) , request_duration smallint unsigned , request_line varchar(255), request_method varchar(10) , request_protocol varchar(10) , request_time char(28), request_uri varchar(255), request_args varchar(255), server_port smallint unsigned, ssl_cipher varchar(25), ssl_keysize smallint unsigned, ssl_maxkeysize smallint unsigned, status smallint unsigned , time_stamp int unsigned , virtual_host varchar(255) ); INSERT INTO access_log_1148226196 VALUES('','Mozilla/5.0 (Windows NT 5.1; U; en) Opera 9.00''13205','6178','None','None','None','[url]http://forum.aqua-computer.de/index.php?page=Thread&threadID=77538','84.163.67.124','None','-','0','GET[/url] /banner_book.gif HTTP/1.1','GET','HTTP/1.1','[21/May/2006:10:27:33 +0000]','/banner_book.gif','None','80','None','None','None','200','1148207253','server') Traceback (most recent call last): File "/www/python/python_test.py", line 54, in ? cursor.execute(sqlstatement) File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 163, in execute self.errorhandler(self, exc, value) File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 35, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.OperationalError: (1136, [b]"Column count doesn't match value count at row 1[/b]") |
![]() |
Source code |
1 |
s = "... VALUES ('" + "', '".join(['%s' for i in range(10)]) + "')" |
![]() |
Source code |
1 |
s= "... VALUES (" + ("'%s', " * 10) + "'%s')" |
![]() |
Source code |
1 2 3 4 5 6 |
..... ..... sqlstatement = "INSERT INTO access_log_"+str(zeit)[0:-3]+" VALUES (" + ("'%s', " * 24) + "\'%s\')" % (str(row[0]), str(row[1]),str(row[2]),str(row[3]),str(row[4]),str(row[5]),str(row[6]),str(row[7]),str(row[8]),str(row[9]),str(row[10]),str(row[11]),str(row[12]),str(row[13]),str(row[14]),str(row[15]),str(row[16]),str(row[17]),str(row[18]), str(row[19]),str(row[20]),str(row[21]),str(row[22]),str(row[23]),str(row[24])) ..... ..... |
![]() |
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
server ~ # python ./python_test.py build 79 79 create table access_log_1148233823 ( id char(19) , agent varchar(255) , bytes_sent int unsigned , child_pid smallint unsigned, cookie varchar(255), machine_id varchar(25), request_file varchar(255), referer varchar(255) , remote_host varchar(50) , remote_logname varchar(50) , remote_user varchar(50) , request_duration smallint unsigned , request_line varchar(255), request_method varchar(10) , request_protocol varchar(10) , request_time char(28), request_uri varchar(255), request_args varchar(255), server_port smallint unsigned, ssl_cipher varchar(25), ssl_keysize smallint unsigned, ssl_maxkeysize smallint unsigned, status smallint unsigned , time_stamp int unsigned , virtual_host varchar(255) ); Traceback (most recent call last): File "/www/python/python_test.py", line 55, in ? sqlstatement = "INSERT INTO access_log_"+str(zeit)[0:-3]+" VALUES (" + ("'%s', " * 24) + "\'%s\')" % (str(row[0]), str(row[1]),str(row[2]),str(row[3]),str(row[4]),str(row[5]),str(row[6]),str(row[7]),str(row[8]),str(row[9]),str(row[10]),str(row[11]),str(row[12]),str(row[13]),str(row[14]),str(row[15]),str(row[16]),str(row[17]),str(row[18]), str(row[19]),str(row[20]),str(row[21]),str(row[22]),str(row[23]),str(row[24])) TypeError: not all arguments converted during string formatting |
![]() |
Source code |
1 |
... % (str(row[0]), str(row[1]),str(row[2]), ... |
![]() |
Source code |
1 |
... % (row) |
![]() |
Source code |
1 |
... % ([str(field) for field in row]) |
![]() |
Source code |
1 |
sqlstatement = "INSERT INTO access_log_"+str(zeit)[0:-3]+" VALUES (" + ("'%s', " * 23) + "'%s')" % ([str(field) for field in row]) |
![]() |
Source code |
1 2 3 4 5 6 7 8 9 |
INSERT INTO access_log_1148235478 VALUES ('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '['', 'Mozilla/5.0 (Windows NT 5.1; U; en) Opera 9.00', '13205', '6178', 'None', 'None', 'None', '[url]http://forum.aqua-computer.de/index.php?page=Thread&threadID=77538',[/url] '84.163.67.124', 'None', '-', '0', 'GET /banner_book.gif HTTP/1.1', 'GET', 'HTTP/1.1', '[21/May/2006:10:27:33 +0000]', '/banner_book.gif', 'None', '80', 'None', 'None', 'None', '200', '1148207253', 'server']') Traceback (most recent call last): File "/www/python/python_test.py", line 57, in ? cursor.execute(sqlstatement) File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 163, in execute self.errorhandler(self, exc, value) File "/usr/lib/python2.4/site-packages/MySQLdb/connections.py", line 35, in defaulterrorhandler raise errorclass, errorvalue _mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Mozilla/5.0 (Windows NT 5.1; U; en) Opera 9.00', '13205', '6178', 'None', 'None'' at line 1") |
![]() |
Source code |
1 2 3 |
sqlstatement_raw = "INSERT INTO access_log_" + str(zeit)[0:-3] + " VALUES (" + ("%s, " * 23) + "%s)" print sqlstatement_raw cursor.execute(sqlstatement_raw, [str(field) for field in row]) |
![]() |
Source code |
1 2 3 4 5 6 7 |
INSERT INTO access_log_1148239197 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) Traceback (most recent call last): File "/www/python/python_test.py", line 59, in ? cursor.execute(sqlstatement_raw, [str(field) for field in row]) File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 148, in execute query = query % db.literal(args) TypeError: not all arguments converted during string formatting |
![]() |
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 |
INSERT INTO access_log_1148241099 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) Traceback (most recent call last): File "/www/python/python_test.py", line 60, in ? cursor.execute(sqlstatement_raw, [str(field) for field in row]) File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 165, in execute self._warning_check() File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 80, in _warning_check warn(w[-1], self.Warning, 3) File "/usr/lib/python2.4/warnings.py", line 61, in warn warn_explicit(message, category, filename, lineno, module, registry) File "/usr/lib/python2.4/warnings.py", line 79, in warn_explicit if registry.get(key): TypeError: unhashable type |
![]() |
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
#!/usr/bin/python import MySQLdb import os import time db = MySQLdb.connect(host="localhost", user="apache", passwd="apache",db="apache") cursor = db.cursor() cursor.execute("SELECT COUNT(*) FROM access_log") row = cursor.fetchone() anz_log = row[0] cursor.execute("DELETE FROM access_log WHERE agent='-'") cursor.execute("DELETE FROM access_log WHERE remote_host='192.168.0.1'") cursor.execute("SELECT COUNT(*) FROM access_log") row = cursor.fetchone() new_anz_log = row[0] zeit = time.time() if anz_log>50: sql_create_statement = "create table access_log_"+str(zeit)[0:-3]+""" ( id char(19) , agent varchar(255) , bytes_sent int unsigned , child_pid smallint unsigned, cookie varchar(255), machine_id varchar(25), request_file varchar(255), referer varchar(255) , remote_host varchar(50) , remote_logname varchar(50) , remote_user varchar(50) , request_duration smallint unsigned , request_line varchar(255), request_method varchar(10) , request_protocol varchar(10) , request_time char(28), request_uri varchar(255), request_args varchar(255), server_port smallint unsigned, ssl_cipher varchar(25), ssl_keysize smallint unsigned, ssl_maxkeysize smallint unsigned, status smallint unsigned , time_stamp int unsigned , virtual_host varchar(255) ); """ cursor.execute(sql_create_statement) cursor.execute("SELECT * FROM access_log WHERE 1") rows = cursor.fetchall() for row in rows: sqlstatement_raw = "INSERT INTO access_log_" + str(zeit)[0:-3] + " VALUES (" + ("%s, " * (len(row)-1)) + "%s)" print sqlstatement_raw cursor.execute(sqlstatement_raw, [str(field) for field in row]) print "fertig" |
![]() |
Source code |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
server ~ # python /www/python/python_test.py INSERT INTO access_log_1148324312 VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) Traceback (most recent call last): File "/www/python/python_test.py", line 51, in ? cursor.execute(sqlstatement_raw, [str(field) for field in row]) File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 165, in execute self._warning_check() File "/usr/lib/python2.4/site-packages/MySQLdb/cursors.py", line 80, in _warning_check warn(w[-1], self.Warning, 3) File "/usr/lib/python2.4/warnings.py", line 61, in warn warn_explicit(message, category, filename, lineno, module, registry) File "/usr/lib/python2.4/warnings.py", line 79, in warn_explicit if registry.get(key): TypeError: [b]unhashable type[/b] |
![]() |
Source code |
1 |
('', 'Mozilla/5.0 (Windows NT 5.1; U; en) Opera 9.00', 13205L, 6178, None, None, None, '[url]http://forum.aqua-computer.de/index.php?page=Thread&threadID=77538',[/url] '84.163.67.124', None, '-', 0, 'GET /banner_book.gif HTTP/1.1', 'GET', 'HTTP/1.1', '[21/May/2006:10:27:33 +0000]', '/banner_book.gif', None, 80, None, None, None, 200, 1148207253L, 'server') |
![]() |
Source code |
1 |
["'%s'" % str(field) for field in row] |
Quoted from "Y0Gi"
Wenn ich das richtig verstehe, würdest du die List Comprehension zu folgender erweitern:
![]()
Source code
1 ["'%s'" % str(field) for field in row]
-