Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Why 'files.py' does not print the filenames into a table format? (http://www.velocityreviews.com/forums/t961805-why-files-py-does-not-print-the-filenames-into-a-table-format.html)

Nick the Gr33k 06-15-2013 07:38 PM

Why 'files.py' does not print the filenames into a table format?
 
Hello,

Trying to browse http://superhost.gr/?page=files.py with tailing -F of
the error_log i noticed that error log outputs no error!

So that means that the script is correct.

here are the directory app's files.

nikos@superhost.gr [~/www/data/apps]# ls -l
total 412788
drwxr-xr-x 2 nikos nikos 4096 Jun 12 12:03 ./
drwxr-xr-x 6 nikos nikos 4096 May 26 21:13 ../
-rwxr-xr-x 1 nikos nikos 13157283 Mar 17 12:57 100\ Mythoi\ tou\
Aiswpou.pdf*
-rwxr-xr-x 1 nikos nikos 29524686 Mar 11 18:17 Anekdotologio.exe*
-rw-r--r-- 1 nikos nikos 42413964 Jun 2 20:29 Battleship.exe
-rw-r--r-- 1 nikos nikos 51819750 Jun 2 20:04 Luxor\ Evolved.exe
-rw-r--r-- 1 nikos nikos 60571648 Jun 2 14:59 Monopoly.exe
-rwxr-xr-x 1 nikos nikos 1788164 Mar 14 11:31 Online\ Movie\ Player.zip*
-rw-r--r-- 1 nikos nikos 5277287 Jun 1 18:35 O\ Nomos\ tou\ Merfy\
v1-2-3.zip
-rwxr-xr-x 1 nikos nikos 16383001 Jun 22 2010 Orthodoxo\ Imerologio.exe*
-rw-r--r-- 1 nikos nikos 6084806 Jun 1 18:22 Pac-Man.exe
-rw-r--r-- 1 nikos nikos 45297713 Jun 10 12:38 Raptor\ Chess.exe
-rw-r--r-- 1 nikos nikos 25476584 Jun 2 19:50 Scrabble.exe
-rwxr-xr-x 1 nikos nikos 49141166 Mar 17 12:48 To\ 1o\ mou\ vivlio\ gia\
to\ skaki.pdf*
-rwxr-xr-x 1 nikos nikos 3298310 Mar 17 12:45 Vivlos\ gia\ Atheofovous.pdf*
-rw-r--r-- 1 nikos nikos 1764864 May 29 21:50 V-Radio\ v2.4.msi
-rw-r--r-- 1 nikos nikos 3511233 Jun 4 14:11 Ευχή\ του\ Ιησού.mp3
-rwxr-xr-x 1 nikos nikos 66896732 Mar 17 13:13 Κοσμάς\ Αιτωλός\ -\
*ροφητείες.pdf*
-rw-r--r-- 1 nikos nikos 236032 Jun 4 14:10 Σκ*ψου\ *ναν\ αριθμό.exe


The code is as follows:

#
================================================== ================================================== =============
# Convert wrongly encoded filenames to utf-8
#
================================================== ================================================== =============
path = b'/home/nikos/public_html/data/apps/'
filenames = os.listdir( path )

utf8_filenames = []

for filename in filenames:
# Compute 'path/to/filename'
filename_bytes = path + filename
encoding = guess_encoding( filename_bytes )

if encoding == 'utf-8':
# File name is valid UTF-8, so we can skip to the next file.
utf8_filenames.append( filename_bytes )
continue
elif encoding is None:
# No idea what the encoding is. Hit it with a hammer until it stops
moving.
filename = filename_bytes.decode( 'utf-8', 'xmlcharrefreplace' )
else:
filename = filename_bytes.decode( encoding )

# Rename the file to something which ought to be UTF-8 clean.
newname_bytes = filename.encode('utf-8')
os.rename( filename_bytes, newname_bytes )
utf8_filenames.append( newname_bytes )

# Once we get here, the file ought to be UTF-8 clean and the Unicode
name ought to exist:
assert os.path.exists( newname_bytes.decode('utf-8') )


# Switch filenames from utf8 bytestrings => unicode strings
filenames = []

for utf8_filename in utf8_filenames:
filenames.append( utf8_filename.decode('utf-8') )

# Check the presence of a database file against the dir files and delete
record if it doesn't exist
cur.execute('''SELECT url FROM files''')
data = cur.fetchall()

for url in data:
if url not in filenames:
# Delete spurious
cur.execute('''DELETE FROM files WHERE url = %s''', url )


#
================================================== ================================================== =============
# Display ALL files, each with its own download button
#
================================================== ================================================== =============
print('''<body background='/data/images/star.jpg'>
<center><img src='/data/images/download.gif'><br><br>
<table border=5 cellpadding=5 bgcolor=green>
''')

try:
cur.execute( '''SELECT * FROM files ORDER BY lastvisit DESC''' )
data = cur.fetchall()

for row in data:
(filename, hits, host, lastvisit) = row
lastvisit = lastvisit.strftime('%A %e %b, %H:%M')

print('''
<form method="get" action="/cgi-bin/files.py">
<tr>
<td> <center> <input type="submit" name="filename" value="%s"> </td>
<td> <center> <font color=yellow size=5> %s </td>
<td> <center> <font color=orange size=4> %s </td>
<td> <center> <font color=silver size=4> %s </td>
</tr>
</form>
''' % (filename, hits, host, lastvisit) )
print( '''</table><br><br>''' )
except pymysql.ProgrammingError as e:
print( repr(e) )

===========================================
PLEASE take a look, its not a huge code, the encoding was of Steven
idea's, so from another thread is a bit more or less already known to
the most of you.

I just want to know why it doesn't print anything.

Thank you and please whoever does not feel like helping, please at least
not spam the thread.

--
What is now proved was at first only imagined!

Nick the Gr33k 06-15-2013 07:51 PM

Re: Why 'files.py' does not print the filenames into a table format?
 
On 15/6/2013 10:46 , Jarrod Henry wrote:
> Nick, at this point, you need to hire someone to do your work for you.


The code is completely ready.
Some detail is missing and its not printing the files as expected.

Irrelevant to my question i just noticed weird behavior about my
pelatologio.py script which can be seen here:

http://superhost.gr/?show=stats

The first 3 files are of my doing.
All the rest are of someone else's that managed to append entries into
my counters database utilizing this code:

================

try:
#find the needed counter for the page URL
cur.execute('''SELECT ID FROM counters WHERE url = %s''', page )
data = cur.fetchone() #URL is unique, so should only be one

if not data:
#first time for page; primary key is automatic, hit is defaulted
cur.execute('''INSERT INTO counters (url) VALUES (%s)''', page )
cID = cur.lastrowid #get the primary key value of the new record
======================

Does someone want to state something?




--
What is now proved was at first only imagined!

Joshua Landau 06-15-2013 08:37 PM

Re: Why 'files.py' does not print the filenames into a table format?
 
On 15 June 2013 20:51, Nick the Gr33k <support@superhost.gr> wrote:
> On 15/6/2013 10:46 μμ, Jarrod Henry wrote:
>>
>> Nick, at this point, you need to hire someone to do your work for you.

>
>
> The code is completely ready.
> Some detail is missing and its not printing the files as expected.


Look, Nick,

A lot of people are frustrated by you. You should understand that. If
you cannot, you need to step back and consider, or you really are a
troll.

Now, obviously it's not going to get you any help to have half of the
forum angry at you. People have stopped helping, at least in large.
This is fine; people here are volunteers. But you want help.

So, Nick, listen. You need to learn how to ask *smart* questions. If
you do, I *guarantee* that people will respect you a lot more. I'll be
willing to give a bit of time to explain what I mean.

1) What is your problem. Not "I want to know why it doesn't print
anything." Here's an example, for some random idea:

> I've written some code to find the first file in a directory which
> is not UTF-8. Lines 40-42 are meant to print out the file found
> to a log ("/home/joshua/.logs/log"). Unfortunately, although
> there is no error, no file is printed to the log.


2) What have you tried? What debugging have you done? For someone of
your skill level, it's also important to tell us what you think your
code is doing. Example:

> I've tried checking for a failure - when there is no non-UTF-8 file
> in the directory the appropriate error is raised. I think this should
> mean that the "else" after the "for" loop would be run, and this
> should run the lines 40-42 above when there *is* a non-UTF-8
> file.


3) If possible, give us an example we can run.

> To make helping easier, I've removed the code that searches the
> directory as I know that works, and instead there's a list of BytesIO
> and StringIO objects that pretend to be them. The bug is still
> there.


Do you see the difference?

> Irrelevant to my question i just noticed weird behavior about my
> pelatologio.py script which can be seen here:
>
> http://superhost.gr/?show=stats
>
> The first 3 files are of my doing.
> All the rest are of someone else's that managed to append entries into my
> counters database utilizing this code:
>
> ================
>
> try:
> #find the needed counter for the page URL
> cur.execute('''SELECT ID FROM counters WHERE url = %s''',
> page )
> data = cur.fetchone() #URL is unique, so should only
> be one
>
> if not data:
> #first time for page; primary key is automatic, hit
> is defaulted
> cur.execute('''INSERT INTO counters (url) VALUES
> (%s)''', page )
> cID = cur.lastrowid #get the primary key
> value of the new record
> ======================
>
> Does someone want to state something?


Sure. Here I go:

What's the question?

Mark Lawrence 06-15-2013 09:29 PM

Re: Why 'files.py' does not print the filenames into a table format?
 
On 15/06/2013 20:38, Nick the Gr33k wrote:
>
> Thank you and please whoever does not feel like helping, please at least
> not spam the thread.
>


Your arrogance clearly has no bounds. This is a public forum and people
can say what they like. You've wasted enough time as it is, so why
don't you simply bugger off.

--
"Steve is going for the pink ball - and for those of you who are
watching in black and white, the pink is next to the green." Snooker
commentator 'Whispering' Ted Lowe.

Mark Lawrence


Chris Angelico 06-15-2013 10:51 PM

Re: Why 'files.py' does not print the filenames into a table format?
 
On Sun, Jun 16, 2013 at 6:29 AM, Benjamin Schollnick
<benjamin@schollnick.net> wrote:
> cur.execute('''SELECT ID FROM counters WHERE url = %s''', page )
> cur.execute('''INSERT INTO counters (url) VALUES (%s)''', page )
>
> Sure, whoever wrote that code is a fool.
>
> http://xkcd.com/327/
>
> They didn't sanitize your database inputs.


I assume you're talking about the above two lines of code? They're not
SQL injection targets. The clue is that the %s isn't in quotes. This
is an out-of-band argument passing method (actually, since he's using
MySQL (IIRC), it's probably just going to escape it and pass it on
through, but it comes to the same thing), so it's safe.

ChrisA

Steven D'Aprano 06-16-2013 12:10 AM

Re: Why 'files.py' does not print the filenames into a tableformat?
 
On Sat, 15 Jun 2013 22:29:29 +0100, Mark Lawrence wrote:

> Your arrogance clearly has no bounds. This is a public forum and people
> can say what they like. You've wasted enough time as it is, so why
> don't you simply bugger off.


Congratulation. You have just entered an extremely exclusive club. See
you in a month.


*plonk*


--
Steven

Nick the Gr33k 06-16-2013 01:03 AM

Re: Why 'files.py' does not print the filenames into a table format?
 
On 16/6/2013 12:29 πμ, Mark Lawrence wrote:
> On 15/06/2013 20:38, Nick the Gr33k wrote:
>>
>> Thank you and please whoever does not feel like helping, please at least
>> not spam the thread.
>>


> Your arrogance clearly has no bounds.


Your spamming to my threads in an unproductive and yet bitching way has
no bounds either.

>This is a public forum


Yes it is.

>and people can say what they like.


Only if its relative to the OP's question, otherwise its trolling to an
other's person thread.

> You've wasted enough time as it is, so why don't you simply bugger off.


The only time i'm wasting is that of folk's trying to respond to my
questions.

You are the one that wants to waste his time if you take the time and
read my posts and also take more time to bitch-respond.

I said to you and others before. Kill-file me, or mute my threads or
ignore me if you do not like me and my questions.



--
What is now proved was at first only imagined!

Nick the Gr33k 06-16-2013 01:07 AM

Re: Why 'files.py' does not print the filenames into a table format?
 
On 16/6/2013 1:51 πμ, Chris Angelico wrote:
> On Sun, Jun 16, 2013 at 6:29 AM, Benjamin Schollnick
> <benjamin@schollnick.net> wrote:
>> cur.execute('''SELECT ID FROM counters WHERE url = %s''', page )
>> cur.execute('''INSERT INTO counters (url) VALUES (%s)''', page )
>>
>> Sure, whoever wrote that code is a fool.
>>
>> http://xkcd.com/327/
>>
>> They didn't sanitize your database inputs.

>
> I assume you're talking about the above two lines of code? They're not
> SQL injection targets.


Then how those page entries found in the database Chris?

> The clue is that the %s isn't in quotes.


What happens if i write it like this?

cur.execute('''SELECT ID FROM counters WHERE url = "%s"''', page )

How quoting of %s helps here?

> This is an out-of-band argument passing method (actually, since he's using
> MySQL (IIRC), it's probably just going to escape it and pass it on
> through, but it comes to the same thing), so it's safe.


Yes iam using a comma and not a substitute operator, so input is mysql
validates.

Please explain what is an "out-of-band argument passing method"

What your idea of those entries made it to the counters database table?


--
What is now proved was at first only imagined!

Nick the Gr33k 06-16-2013 01:10 AM

Re: Why 'files.py' does not print the filenames into a table format?
 
On 16/6/2013 1:51 πμ, Chris Angelico wrote:
> On Sun, Jun 16, 2013 at 6:29 AM, Benjamin Schollnick
> <benjamin@schollnick.net> wrote:
>> cur.execute('''SELECT ID FROM counters WHERE url = %s''', page )
>> cur.execute('''INSERT INTO counters (url) VALUES (%s)''', page )
>>
>> Sure, whoever wrote that code is a fool.
>>
>> http://xkcd.com/327/
>>
>> They didn't sanitize your database inputs.

>
> I assume you're talking about the above two lines of code? They're not
> SQL injection targets. The clue is that the %s isn't in quotes. This
> is an out-of-band argument passing method (actually, since he's using
> MySQL (IIRC), it's probably just going to escape it and pass it on
> through, but it comes to the same thing), so it's safe.
>
> ChrisA
>


Chris or someone else please explain a bit whats happening here because
that list is getting bigger and bigger as we speak.

look: http://superhost.gr/?show=stats

At least i have secured 'pelatologio.py' form prying eyes.

--
What is now proved was at first only imagined!

Mark Lawrence 06-16-2013 01:10 AM

Re: Why 'files.py' does not print the filenames into a table format?
 
On 16/06/2013 02:03, Nick the Gr33k wrote:
> On 16/6/2013 12:29 πμ, Mark Lawrence wrote:
>> On 15/06/2013 20:38, Nick the Gr33k wrote:
>>>
>>> Thank you and please whoever does not feel like helping, please at least
>>> not spam the thread.
>>>

>
>> Your arrogance clearly has no bounds.

>
> Your spamming to my threads in an unproductive and yet bitching way has
> no bounds either.
>
>> This is a public forum

>
> Yes it is.
>
>> and people can say what they like.

>
> Only if its relative to the OP's question, otherwise its trolling to an
> other's person thread.
>
>> You've wasted enough time as it is, so why don't you simply bugger off.

>
> The only time i'm wasting is that of folk's trying to respond to my
> questions.
>
> You are the one that wants to waste his time if you take the time and
> read my posts and also take more time to bitch-respond.
>
> I said to you and others before. Kill-file me, or mute my threads or
> ignore me if you do not like me and my questions.
>


I have no intention of kill-filing you, muting your threads or ignoring
you. I do intend hounding you until with any luck you crawl off into a
hole somewhere and leave this group in peace.

--
"Steve is going for the pink ball - and for those of you who are
watching in black and white, the pink is next to the green." Snooker
commentator 'Whispering' Ted Lowe.

Mark Lawrence



All times are GMT. The time now is 12:16 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.