Trouble opening files

  • Thread starter Westbrook, Christopher L (WESTBCL04)
  • Start date
W

Westbrook, Christopher L (WESTBCL04)

I am having some trouble, and I can't figure out why. I'm trying to use
the simple keylogger project from source forge, and then create a web
interface to open the resulting files, so people from the outside can
see the keys logged on a server. I've got the keylogger script working
fine, and I have a script that reads and displays all the directories
and files of the log directory, but when I select the link of the file
it doesn't open. I've made some changes to the pykeylogger source,
mainly using @@ between the date and window name rather than _ and
getting rid of the window handle. First, I have placed the script that
makes the page with the lists of the directories and files, and then
I've pasted the script that opens the file, or at least attempts to.
Can someone help?
#!c:\python24\python.exe
import os
print"Content-type:text/html\n\n";
print"<html<head><title>directory list</title></head><body>"
for f in os.listdir("c:\\temp\\logdir"):
print "<h1>",f,"</h1>\n"
subdir = os.path.join("c:\\temp\\logdir",f)
#debug info here
print "<br/>subdir=",subdir


for g in os.listdir(subdir):
print "<br/>g= ",g
file = os.path.splitext(g)[0]
print "<br/>file=",file
fullPath = os.path.join(subdir,file)
[date, name] = file.split('@@')

print "<a
href=\"read.py?file=",fullPath,"\">",name,"</a>"
print "<br/>last modified: ",date

#now for the script that opens the file
#!c:\\python24\\python.exe

#import re
import cgi
import string

form = cgi.FieldStorage()
print "Content-type:text/html\n\n";
filename = form["file"].value+".txt" #all files end in .txt extension

filename = filename.replace("\\","\\\\")

#print filename

f = file(filename,'\r') #open file for reading
for line in f:
#replace carriage returns with html newline characters
line = line.replace("\r","<br/>")
print line

I'm running this on a windows machine. Any help would be appreciated.
 
T

Tim Roberts

Westbrook said:
I am having some trouble, and I can't figure out why.
....

form = cgi.FieldStorage()
print "Content-type:text/html\n\n";
filename = form["file"].value+".txt" #all files end in .txt extension

filename = filename.replace("\\","\\\\")

This line is incorrect. Backslashes are only treated as escape characters
when they are LITERAL strings in a Python source file. When you're reading
a filename from some outside source like this, the string will already
contain the correct characters.

What you'll end up with is literally double backslashes. For example, if
you read a variable containing c:\tmp\new.txt, you will end up with
c:\\tmp\\new.txt, AS IF you had given the literal string
"c:\\\\tmp\\\\new.txt".

Fortunately, most Win32 APIs will ignore double-backslashes, but it isn't
right. And, they are guaranteed not to work if they are the first
characters of the filename.
#print filename

f = file(filename,'\r') #open file for reading

Where did you get that? \r is a carriage return. You need a letter "r":

f = file(filename, 'r')
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top