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
index.txt.
links.txt.
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?
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.
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.
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