Trouble opening files

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Westbrook, Christopher L (WESTBCL04)

    Trouble opening files

    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\p ython.exe
    import os
    print"Content-type:text/html\n\n";
    print"<html<hea d><title>direct ory 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=",subdi r


    for g in os.listdir(subd ir):
    print "<br/>g= ",g
    file = os.path.splitex t(g)[0]
    print "<br/>file=",file
    fullPath = os.path.join(su bdir,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.FieldStorag e()
    print "Content-type:text/html\n\n";
    filename = form["file"].value+".txt" #all files end in .txt extension

    filename = filename.replac e("\\","\\\\" )

    #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.

  • Tim Roberts

    #2
    Re: Trouble opening files

    "Westbrook, Christopher L (WESTBCL04)" <WESTBCL04@juni ata.edu> wrote:[color=blue]
    >
    >I am having some trouble, and I can't figure out why.[/color]

    ....
    [color=blue]
    >form = cgi.FieldStorag e()
    >print "Content-type:text/html\n\n";
    >filename = form["file"].value+".txt" #all files end in .txt extension
    >
    >filename = filename.replac e("\\","\\\\" )[/color]

    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.tx t, AS IF you had given the literal string
    "c:\\\\tmp\\\\n ew.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.
    [color=blue]
    >#print filename
    >
    >f = file(filename,' \r') #open file for reading[/color]

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

    f = file(filename, 'r')
    --
    - Tim Roberts, timr@probo.com
    Providenza & Boekelheide, Inc.

    Comment

    Working...