A minimal gopher server protocol implementation in python
This is a minimalist implementation of a working gopher server in Python programming language. It is buggy but it works
#!/usr/bin/env python
# gopherd.py – A Simple Gopher Server
# By Maxin B. John (maxinbjohn@gmail.com)
#
# This little program illustrates the gopher protocol implementation
# using python with the help of SocketServer module.
#
# The gopher protocol is a very simple TCP-based protocol;which listen
# on port number 70.The Internet Gopher protocol is designed for
# distributed document search and retrieval.
#
# The Internet Gopher protocol is designed primarily to act as a
# distributed document delivery system. While documents (and services)
# reside on many servers, Gopher client software presents users with a
# hierarchy of items and directories much like a file system.
import SocketServer
import string
import os
class GopherHandler(SocketServer.StreamRequestHandler):
def handle(self):
# Read a line of text, limiting it to 512 bytes.
# This will prevent someone trying to crash the server machine
# by sending megabytes of data.
searchstring=self.rfile.readline(512)
print ‘searching ‘+ searchstring
# Remove any leading and trailing whitespace, including the
# trailing newline.
searchstring=string.strip(searchstring)
# invoking the get_directory_info to get the return string
info = self.get_directory_info(searchstring)
# send the data back to gopher client
self.wfile.write(info)
print ‘printing the info \n’+ info
# The following method takes a string containing the searchstring
# This function will search the directory and provide the information
# in Gopher Protocol format. Later will be expanded to contain the searching
# logic based on the searchstring.
def get_directory_info(self, searchstring):
info = ”
dir_path=os.getcwd()
#Testing whether the request is for a particular file or folder
#if (searchstring == “” or os.path.isdir(searchstring)== 1):
if (searchstring == “”):
“Return a string containing the file and directory information in gopher protocol format.”
file_list= os.listdir(os.getcwd())
for fileName in file_list:
if os.path.isfile(fileName) == 1:
info += ’0′+fileName+’\t’+fileName+’\tlocalhost\t70\r\n’
elif os.path.isdir(fileName) == 1:
info += ’1′+fileName+’\t’+fileName+’\tlocalhost\t70\r\n’
else:
print ‘This is a link ‘ +fileName
info += ’2′+fileName+’\t’+fileName+’\tlocalhost\t70\r\n’
return “%s\r\n” % (info,)
else:
fileName = searchstring
if os.path.isfile(fileName) == 1:
info = self.handle_file(fileName)
elif os.path.isdir(fileName) == 1:
info = self.handle_folder(fileName)
else:
info = ‘nothing’
return “%s\r\n” % (info,)
# Function to handle the request for a file
def handle_file(self, fileName):
fd = open(fileName,’r')
textcontent = fd.read()+’\r\n’
return “%s \r\n” %(textcontent,)
# Function to handle the request for a folder
def handle_folder(self, files):
info = ”
os.chdir(os.getcwd()+’/'+files)
file_list= os.listdir(os.getcwd())
for fileName in file_list:
if os.path.isfile(fileName) == 1:
info += ’0′+fileName+’\t’+fileName+’\tlocalhost\t70\r\n’
elif os.path.isdir(fileName) == 1:
info += ’1′+fileName+’\t’+fileName+’\tlocalhost\t70\r\n’
else:
print ‘This is a link ‘ +fileName
info += ’2′+fileName+’\t’+fileName+’\tlocalhost\t70\r\n’
return “%s\r\n” % (info,)
# If this script is being run directly, it’ll start acting as a gopher
# daemon. The following “if” statement is the usual Python style for
# running code only as a stand alone script.
if __name__==’__main__’:
try:
# Create an instance of our server class
server=SocketServer.ForkingTCPServer( (”, 70), GopherHandler)
# Enter an infinite loop, waiting for requests and then servicing them.
server.serve_forever()
# For graceful exit when ^c is used.
except KeyboardInterrupt:
print “Received Keyboard interrput: Exiting…”
exit
) .. Gopher protocol is defined in the RFC 1436. It is a TCP based protocol listening on port 70. So using these , I have decided to give it a try .. Got a half baked gopher …It is under construction , but being a believer of the the Free Software Philosophy, I am Publishing it (even if it is not complete )..