jgoerzen / pygopherd

Multiprotocol Gopher/Web Server [Python]
GNU General Public License v2.0
170 stars 25 forks source link

no attribute PYGmain #9

Open lefalaf opened 3 years ago

lefalaf commented 3 years ago

Hi,

I'm trying to use PYGhandler and generate a very simple print() just to see what happen so far my file test.pyg looks like this

#!/usr/bin/python
print('test');

But I get this error

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/pygopherd/initialization.py", line 82, in handle
  File "/usr/lib/python2.7/dist-packages/pygopherd/protocols/base.py", line 85, in handle
  File "/usr/lib/python2.7/dist-packages/pygopherd/handlers/UMN.py", line 60, in prepare
  File "/usr/lib/python2.7/dist-packages/pygopherd/handlers/dir.py", line 92, in prepare
  File "/usr/lib/python2.7/dist-packages/pygopherd/handlers/dir.py", line 66, in prep_entries
  File "/usr/lib/python2.7/dist-packages/pygopherd/handlers/HandlerMultiplexer.py", line 60, in getHandler
  File "/usr/lib/python2.7/dist-packages/pygopherd/handlers/base.py", line 110, in isrequestforme
  File "/usr/lib/python2.7/dist-packages/pygopherd/handlers/pyg.py", line 20, in canhandlerequest
AttributeError: 'module' object has no attribute 'PYGMain'

Am I missing something in my file ?

Thanks !

michael-lazar commented 3 years ago

Hi!

PYGMain is actually the class that you need to implement inside of the .pyg file. The methods of the class are hooks into the application that pygopherd will invoke. There's a complicated example of how this works in the pygfarm directory.

Here's a simple hello world example:

from pygopherd.handlers.pyg import PYGBase
from pygopherd.gopherentry import GopherEntry

class PYGMain(PYGBase):
    def canhandlerequest(self):
        return True

    def isdir(self):
        return False

    def getentry(self):
        entry = GopherEntry(self.selector, self.config)
        entry.type = '0'
        entry.mimetype = 'text/plain'
        entry.name = 'My custom .pyg handler!'
        return entry

    def write(self, wfile):
        wfile.write("hello world!".encode())

If you're looking for something that just works like a CGI script, you can use the scriptexec.ExecHandler instead. That one will set some environment variables for you and you can just write directly to stdout from inside of the script.

lefalaf commented 3 years ago

Thank you @michael-lazar !

Pygfarm example is indeed complicated, but I do understand better now what pyg files are and how they work.

Regarding Exechandler, I assume files must be executables python files ?

michael-lazar commented 3 years ago

no problem!

Regarding Exechandler, I assume files must be executables python files ?

They can be any kind of file (python, bash, perl, etc.) as long as the file is set to executable and has the shebang at the top. The catch is that the Exechandler can only generate text/plain files as the output, so it can't be used to generate proper gopher directories.