#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    2
    Rep Power
    0

    Unhappy pYTHON with http ???????


    I dont know anything about python ,so i begin to read manuals etc from various locations.
    But i have a basic question here, please if someone know help
    Thanks .

    Look at this :

    <a href=\"file://", lease, "\">
    The "lease" is just a variable that receive a IP address that my python read from a file.
    But when i see in the page appear with a space like this
    file:// ipaddress
    -------|---------
    This space can't exist !!!!

    How can I avoid this little space problem ? It's just a simple program to read my dhcpd.leases and take the IP'S and Name's of people connected then show into a page.
    When the person click on the IP then the browser open the shared resources .
    Vary simple, but i don't know how avoid this

    Thanks alot !!!

    Carlos.

    Here is the complete file .
    ----------------------------------

    #!/usr/bin/python
    import string, os
    from sys import stdout

    #-------
    #LeaseFile = "/etc/dhcpd.leases" # if you have Redhat before 6.1
    LeaseFile = "/var/lib/dhcp/dhcpd.leases" # if you have Redhat 6.1 or later

    #------- Markers
    # Entries in the /etc/dhcpd.leases file start with the word 'lease',
    # may span several lines, and then end with a '}'. Lines that start
    # with a TAB (\t) descibe the lease, i.e. mac address, start, etc.
    newleaseMarker = 'l' # lease starts with 'l'
    endleaseMarker = '}' # ends with a '}'
    infoMarker = '\t' # lease informational lines start with a TAB
    startsMarker = '\ts' # \tstarts
    endMarker = '\te' # \tend
    abandonedMarker = '\ta' # \tabandoned
    clientMarker = '\tc' # \tclient
    uidMarker = '\tu' # \tuid
    hardwareMarker = '\th' # \thardware

    # ------- Date arrays (pretty print the date)
    weekday = ["Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"]
    nthday = ["", "1st", "2nd", "3rd", "4th", "5th", "6th", "7th", "8th", "9th", "10th", "11th", "12th", "13th",
    "14th", "15th", "16th", "17th", "18th", "19th", "20th", "21st", "22nd", "23rd", "24th", "25th", "26th", "27th
    ", "28th", "29th", "30th", "31st"]

    #------- HTML Setup
    print "Content-Type: text/html\n\n" # CGI header
    print "<HTML><HEAD><LINK REL=\"stylesheet\" HREF=\"/lanlord/lanlord.css\">"

    #------- Start the real work
    indate = os.popen('date +"%A %B %m %r %Y"', 'r') # Get todays date
    datedata = string.split(indate.readline())
    today = string.atoi(datedata[2]) -1
    print "</HEAD><BODY BGCOLOR=\"#ffffff\">"
    print "<h3>DHCP Lease Report for", datedata[0], datedata[1], nthday[(today)], datedata[3], datedata[5], dated
    ata[4], "</h3>"
    input = open(LeaseFile, 'r') # Open the leases file

    L = { } # Initialize hash

    for line in input.readlines():
    if line[:1] != newleaseMarker:
    if line[:1] != endleaseMarker:
    if line[:2] == startsMarker:
    sDayLeased = line[8:9]
    sDateLeased= line[10:20]
    sHourLeased = line[21:len(line) -2]
    elif line[:2] == endMarker:
    eDayLeased = line[6:7]
    eDateLeased= line[8:18]
    eHourLeased = line[19:len(line) -2]
    elif line[:2] == abandonedMarker:
    macLeased = "<em>Abandoned</em>"
    elif line[:2] == clientMarker:
    clientLeased = "<b>" +line[18:len(line) -3] + "</b>"
    elif line[:2] == uidMarker:
    pass # get any MAC address
    off hardware line
    elif line[:2] == hardwareMarker:
    macLeased = line[19:len(line) -2]
    else:
    ## print "**UNKOWN**", # Not one of the 'usual suspe
    cts'
    pass
    else:
    sd = string.atoi(sDayLeased)
    ed = string.atoi(eDayLeased)
    entry = macLeased + "*" + clientLeased + "*" + weekday[sd] + "*" + sDateLeased + "*"
    + sHourLeased + "*" + weekday[ed] + "*" + eDateLeased + "*" + eHourLeased
    L[ipLeased] = {`ipLeased` : `entry`}

    else:
    ipLeased = line[6:len(line) -3]
    clientLeased = 'None'
    macLeased = 'None'

    print "<h5>", len(L), " leases</h5><Table border=\"1\"><th>IP Address</th><th>Hostname</th><th>Lease Start</t
    h><th>Lease End</th><th>MAC Address</th>"
    dhcpLease = L.keys()
    dhcpLease.sort()
    for lease in dhcpLease:
    infoLease = L[lease]
    dataLease = infoLease.values()
    infoLease = dataLease
    lLease = string.splitfields(`infoLease`,"*")
    lMac = lLease[0][3:21]
    lDate = lLease[7][0:8]
    print "<TR><td>", lease, "</td><td>", lLease[1],"</td><td>", lLease[2], lLease[4], lLease[3], "</td><
    td>", lLease[5], lDate, lLease[6], "</td><td>",lMac,"</td></tr>\n"

    print "</table><p><a href=\"file://", ipLeased, "\">Test Part</a></p></body></html>"

    - End file

    Just rename it into the cgi-bin with name test.cgi to see it working.
    Can someone help here !!!
    Thanks again .
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69

    Talking +


    Hi carnt, the problem is that when you join a string with a , then the space btween that and the variable counts i.e.

    print "hey", variable, "."

    if you replace the , with a + then the spacd shouldnt be a problem.

    Hope this is what you were looking for.

    Take care,
    Mark.
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2003
    Posts
    2
    Rep Power
    0

    That's right !!! It's work !


    Yep !
    Thanks alot !!!!!!!!!!!!

IMN logo majestic logo threadwatch logo seochat tools logo