Welcome, guest | Sign In | My Account | Store | Cart

The following allows someone to create "smart" links on their web site. This is just a simple system and is not very advanced, but it works quite well (given its purpose). If a link is not active (because a server is down), the link is shown as such.

Python, 70 lines
 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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
def main(index, links, key):
    try:
        index = file(index, 'rU', 0).read()
        try:
            links = file(links, 'rU', 0).read()
            try:
                source = build(index, links, key)
                try:
                    html(source)
                except:
                    plain('ERROR: the source could not be displayed.')
            except:
                plain('ERROR: the source could not be built.')
        except:
            plain('ERROR: %s (LINKS) cannot be found.' % links)
    except:
        plain('ERROR: %s (INDEX) cannot be found.' % index)

def build(index, links, key):
    links = parse(links)
    links = check(links)
    links = write(links)
    index = final(index, links, key)
    return index

def parse(links):
    links = links.splitlines()
    for index in range(len(links)):
        links[index] = links[index].split(' ', 1)
    return links

def check(links):
    from socket import socket
    for index in range(len(links)):
        try:
            test = socket()
            test.settimeout(0.05)
            test.connect((links[index][0], 80))
        except:
            links[index][0] = None
    return links

def write(links):
    string = str()
    for link in links:
        string += '\t<h3>\n\t\t'
        if link[0] is None:
            string += link[1]
        else:
            string += '<a href="http://' + link[0] + '/">' + link[1] + '</a>'
        string += '\n\t</h3>\n'
    return string [:-1]

def final(index, links, key):
    key = '<!--' + key + '-->'
    index = index.replace(key, links)
    return index

def html(string):
    print 'Content-type: text/html'
    print
    print string

def plain(string):
    print 'Content-type: text/plain'
    print
    print string

if __name__ == '__main__':
    main('index.txt', 'links.txt', 'Python: Insert Links')

The following files show how this code can be used.

index.txt

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><HTML><HEAD>
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html; charset=iso 8859-1">
<TITLE>CGI Playground</TITLE>
<STYLE type="text/css">
<!--
BODY {
    FONT-SIZE: small; COLOR: #000000; FONT-FAMILY: "trebuchet ms", Verdana, Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #FFFFFF
}

H1 {
    FONT-SIZE: large;
}

A:hover {
    COLOR: #ff9900; TEXT-DECORATION: underline;
}

.footer {
    TEXT-ALIGN: center; FONT-SIZE: smaller;
}

.footer IMG {
    BORDER: 1px solid #888;
}
-->
</STYLE>
</HEAD>
<BODY bgColor="#ffffff" text="#000000">
<H1 align="center">Welcome to Abyss Web Server</H1>
<P>
<B>Abyss Web Server</B> is running correctly on your system. You should now change this page with yours.
</P>
<P>
Please include in your web pages (at least the first) the <b><i>'Powered by Abyss Web Server'</i></b> banner to promote the use of the software.
</P>
<hr>
<CENTER>
<P CLASS="footer">
Abyss Web Server - 
Copyright &#169; 2001-2005 <A HREF="http://www.aprelium.com" >Aprelium Technologies</A> - All rights reserved
</P>
<P CLASS="footer">
<A HREF="http://www.aprelium.com" ><IMG SRC="pwrabyss.gif" ALT="Powered by Abyss Web Server" TITLE="Powered by Abyss Web Server" BORDER="0" WIDTH="88" HEIGHT="31"></A>
</P>
    <hr>
    <h2>
        Why is this the CGI Playground?
    </h2>
    <h3>
        <a href="/python/">Here</a> you will find several different CGI scripts written in Python.<br>
        For your convenience, a <a href="/python/help.html">page of descriptions</a> has been setup.
    </h3>
    <h3>
        You also have access to <a href="/C/">Volume C</a> and <a href="/Z/">Volume Z</a>.
    </h3>
    <h3>
        If you are wondering about the links below,<br>
        just take a look at the <a href="/python/python_script_viewer.py?file=C%3A%5CProgram+Files%5CAbyss+Web+Server%5Chtdocs%5Cindex.py">source code</a>.
    </h3>
    <hr>
    <h2>
        Links:
    </h2>
<!--Python: Insert Links-->
    <hr>
    <h2>
        Door Signs:
    </h2>
    <h3>
        <a href="/door_signs/johnson_211/">Johnson 211</a>
    </h3>
    <h3>
        <a href="/door_signs/johnson_212/">Johnson 212</a>
    </h3>
    <hr>
    <h2>
        Python Games:
    </h2>
    <h3>
        <a href="DynamiteSetup.exe">Dynamite</a>
    </h3>
    <h3>
        <a href="SolarWolf.exe">Solar Wolf</a>
    </h3>
    <hr>
</CENTER>
</BODY></HTML>

links.txt

rmelc208.dorms.bju.edu Randy Melchert
mtuck322.dorms.bju.edu Mark Tucker
pbeni427-web.dorms.bju.edu Peter Beninate
zcoch215.dorms.bju.edu Zachary Cochran
kcruc212.dorms.bju.edu Kevin Cruce
nblac490.dorms.bju.edu Nathanael Black
ahamm894.dorms.bju.edu Aaron Hammons
jcris964.dorms.bju.edu Jonathan Crisan
kstil371.dorms.bju.edu Kennie Still
mangl442.dorms.bju.edu Melody Anglea
aangl403.dorms.bju.edu Andy Anglea
npard220.dorms.bju.edu Nathaniel Pardington
mberg338.dorms.bju.edu Mary Berg
tscot390.dorms.bju.edu Tyler Scott
jjame471.dorms.bju.edu Justin James
jpitt678.dorms.bju.edu James Pitts0

6 comments

Stephen Chappell (author) 18 years, 7 months ago  # | flag

index.txt.

CGI Playground

<!--
BODY {
    FONT-SIZE: small; COLOR: #000000; FONT-FAMILY: "trebuchet ms", Verdana, Arial, Helvetica, sans-serif; BACKGROUND-COLOR: #FFFFFF
}

H1 {
    FONT-SIZE: large;
}

A:hover {
    COLOR: #ff9900; TEXT-DECORATION: underline;
}

.footer {
    TEXT-ALIGN: center; FONT-SIZE: smaller;
}

.footer IMG {
    BORDER: 1px solid #888;
}
-->



Welcome to Abyss Web Server
<p>
Abyss Web Server is running correctly on your system. You should now change this page with yours.

<p>
Please include in your web pages (at least the first) the 'Powered by Abyss Web Server' banner to promote the use of the software.



<p class="footer">
Abyss Web Server -
Copyright &#169; 2001-2005 Aprelium Technologies - All rights reserved

<p class="footer">




        Why is this the CGI Playground?


        This place is all about testing Python CGI files.


        You also have access to Volume C and Volume Z.


        If you are wondering about the links below,

        just take a look at the source code.



        Links:




        Door Signs:


        Johnson 211


        Johnson 212
Stephen Chappell (author) 18 years, 7 months ago  # | flag

links.txt.

rmelc208.dorms.bju.edu Randy Melchert
mtuck322.dorms.bju.edu Mark Tucker
pbeni427-web.dorms.bju.edu Peter Beninate
zcoch215.dorms.bju.edu Zachary Cochran
kcruc212.dorms.bju.edu Kevin Cruce
nblac490.dorms.bju.edu Nathanael Black
ahamm894.dorms.bju.edu Aaron Hammons
jcris964.dorms.bju.edu Jonathan Crisan
kstil371.dorms.bju.edu Kennie Still
mangl442.dorms.bju.edu Melody Anglea
Stephen Chappell (author) 18 years, 7 months ago  # | flag

Sorry About That. I was trying to show the contents of the index.txt file, but it is HTML source. Does anyone have a suggestion as to how I might be able to post the source here?

Yair Chuchem 18 years, 7 months ago  # | flag

The infinite recursion loop hole. Let's think about pages linking to each other as directed graphs.

And let's say that link-pages made with a recipe such as this link to other link-pages of a similar kind.

If the graph of linking has cycles: then pages linking (even indirectly) to pages in cycles would never show up.

Instead the various web hosts will start an endless parade of sending each other requests.

This cool feature will probably be better if implemented in the browser instead of in websites.

Stephen Chappell (author) 18 years, 1 month ago  # | flag

Not a problem. Notice that this program only makes a connection to another web server. It does not actaully make a request. Therefore, Python would not be invoked on each server that is checked.

Stephen Chappell (author) 18 years, 1 month ago  # | flag

Not A Problem. Notice that this program only makes

a connection to another web server.

It does not actaully make a request.

Therefore, Python would not be invoked

on each server that is checked.