Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

dotcomboom
dotcomboom

Posted on • Edited on

     

Pituophis: Setting up a Gopher server

This is a tutorial for the Gopher library I wrote calledPituophis, and is current as of version 0.95. While the API is more or less finalized, it may still be subject to change.

Setting up a server

Installing Pituophis

If you haven't already, install Pituophis by runningpip3 install pituophis, or the equivalent for your Python 3 setup. You will most likely need Python 3.7 or later, because of its usage of asynchronous socket connections.

The base script

First, you'll need to crack open whatever comfortable environment you have and create a new Python file. Save it to a new directory, and then create a new one named "pub" inside of the same directory.

This is what should go in the file:

importpituophispituophis.serve('127.0.0.1',70,pub_dir='pub/')
Enter fullscreen modeExit fullscreen mode

For a Gopher server that will serve files from thepub directory, that's it! Pituophis will also send the gophermap file in each directory if it's available. To run the server just save and then start the script. If you're using a port that is below 1024 (as port 70 is) you may need to run it with elevated privileges .

The host, which is127.0.0.1 in the example, should be changed to whatever host clients will be connecting on. If you're only going to use the server on your machine, it's fine to leave it that, but if you're running it on a private or public IP address or domain name change it to that.

Alt handlers

Pituophis also lets you use an alt handler, a function that will take in requests and then spit out what to send back. If Pituophis, for example, notices that a path does not exist in the publish directory, it will hand off the request to the alt handler.

To use an alt handler, first define it and set it to be used:

importpituophisdefalt(request):returnFalsepituophis.serve('127.0.0.1',70,pub_dir='pub/',alt_handler=alt)
Enter fullscreen modeExit fullscreen mode

The alt handler can return False if it can't do anything with the request, so Pituophis will send back an error as usual.

This example uses the alt handler to send back a menu displaying the request's attributes and a couple selectors:

importpituophisdefalt(request):if(request.path=='/stats'):menu=[pituophis.Selector(text="Path:"+request.path),pituophis.Selector(text="Query:"+request.query),pituophis.Selector(text="Host:"+request.host),pituophis.Selector(text="Port:"+str(request.port)),pituophis.Selector(text="Client:"+request.client),pituophis.Selector(),pituophis.Selector(itype="I",text="View server.png",path="/server.png",host=request.host,port=request.port),pituophis.Selector(itype="0",text="View some text",path="/txt",host=request.host,port=request.port)]returnmenureturnFalsepituophis.serve('127.0.0.1',70,pub_dir='pub/',alt_handler=alt)
Enter fullscreen modeExit fullscreen mode

Remember, if a directory or file calledstats exists in the publish directory, the alt handler will not run, and that static file will be sent instead.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Gopher wrangler, Python yellow belt, snake probably
  • Joined

More fromdotcomboom

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp