# python-fetcher() Source Driver

### Important Information <a href="#important-information" id="important-information"></a>

* Fetcher-style sources that actively fetch messages. In general, write fetcher-style sources (for example, when using simple blocking APIs), unless you explicitly need a server-style source.<br>
* The Python block must be a top-level block in the syslog-ng OSE configuration file.<br>
* If you store the Python code in a separate Python file and only include it in the syslog-ng OSE configuration file, make sure that the PYTHON\_PATH environment variable includes the path to the Python file, and export the PYTHON\_PATH environment variable.

Python sources consist of two parts. The first is a syslog-ng OSE source object that you define in your syslog-ng OSE configuration and use in the log path. This object references a Python class, which is the second part of the Python source. The Python class receives or fetches the log messages, and can do virtually anything that you can code in Python. You can either embed the Python class into your syslog-ng OSE configuration file, or store it in an external Python file. For this test, we will embed the Python class into the configuration file.

### Status <a href="#status" id="status"></a>

| Architecture | Status |
| ------------ | ------ |
| x86          | Works  |
| ARM          | Works  |

### Configuration File Used <a href="#configuration-file-used" id="configuration-file-used"></a>

```
@version: 3.33
@include "scl.conf"

destination console{
    file(/dev/stdout);
};

source s_python{
    python-fetcher(
        class("TestPython")
    );
};

python {
from syslogng import LogFetcher
from syslogng import LogMessage

class TestPython(LogFetcher):

    def fetch(self): # mandatory
        # return LogFetcher.FETCH_ERROR,
        # return LogFetcher.FETCH_NOT_CONNECTED,
        # return LogFetcher.FETCH_TRY_AGAIN,
        # return LogFetcher.FETCH_NO_DATA,
        return (LogFetcher.FETCH_SUCCESS, "Message from Python-Fetcher Source")

    def request_exit(self):
        print("request_exit")
        # If your fetching method is blocking, do something to break it
        # For example, if it reads a socket: socket.shutdown()
};

log {
    source(s_python);
    destination(console);
};
```

### Proof

![python-fetcher() source tested on macOS (x86)](https://3267548193-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MaZdBoDvCx_0JwOUICk%2F-MhAwcRQ5DGTpYdducE8%2F-MhB0BVWj5kB5lheY7MH%2FScreenshot%202021-08-16%20at%201.51.48%20AM.png?alt=media\&token=cb9f5d4b-c644-41e7-82ad-3c673f0725fe)

![python-fetcher() source tested on macOS (x86)](https://3267548193-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-MaZdBoDvCx_0JwOUICk%2F-MhAwcRQ5DGTpYdducE8%2F-MhB0LWJ-txoG1PiT03D%2FScreen%20Shot%202021-08-16%20at%201.52.45%20AM.png?alt=media\&token=6a4b40b2-0c08-475d-afc1-e64df6279349)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://syslog-macos-testing.gitbook.io/syslog-macos-testing/modules/mod-python-7/python-fetcher-source-driver.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
