ActiveState Code

Recipe 440647: "Smart" Links


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
 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')

Discussion

The following files show how this code can be used.

Comments

  1. 1. At 7:49 a.m. on 4 oct 2005, Stephen Chappell (the author) said:

    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
    
  2. 2. At 7:50 a.m. on 4 oct 2005, Stephen Chappell (the author) said:

    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
    
  3. 3. At 7:51 a.m. on 4 oct 2005, Stephen Chappell (the author) said:

    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?

  4. 4. At 12:13 p.m. on 8 oct 2005, Yair Chuchem said:

    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.

  5. 5. At 6:57 a.m. on 11 mar 2006, Stephen Chappell (the author) said:

    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.

  6. 6. At 6:58 a.m. on 11 mar 2006, Stephen Chappell (the author) said:

    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.

Sign in to comment