Python Flask App Gives Error When Running as a Service

  flask, python-3.x, urllib

I am relatively new to this so please bear with me.

Details:
RHEL 7.5
Python 3.6

I have a Python flask app that I want to use to return a set of dynamic job options for Rundeck. The flask app looks like this:

#!/usr/bin/python3

from flask import Flask, request
from get_snow_tickets import get_tickets

app = Flask(__name__)

@app.route('/options', methods=['GET'])
def options():
    ticket_type = request.args.get('type', default='test', type=str)
    query = request.args.get('query', default='test', type=str)
    options = get_tickets(ticket_type, query)
    return options

if __name__ == '__main__':
    app.run(host='127.0.0.1', port='5000', debug=True)

As you can see, it calls another module and feeds it the parameters fed to it in the HTTP GET. The other app will call the ServiceNow API and return some ticket details in JSON format.

This all works when the flask app is run manually/directly – both curl and Rundeck display the desired tickets.

However, when I run this as a service on the RHEL 7 box, I get an error both using curl and Rundeck. The service file looks like this:

[Unit]
Description=Dynamic ServiceNow Job Options for Rundeck
After=network.target

[Service]
User=root
WorkingDirectory=/path/to/directory
ExecStart=/path/to/directory/flask_testing.py
Restart=always

[Install]
WantedBy=multi-user.target

The errors seen:

Traceback (most recent call last):
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2464, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2450, in wsgi_app
    response = self.handle_exception(e)
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1867, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 2447, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1952, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1821, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.6/site-packages/flask/_compat.py", line 39, in reraise
    raise value
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1950, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.6/site-packages/flask/app.py", line 1936, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/path/to/directory/flask_testing.py", line 12, in options
    options = get_tickets(ticket_type, query)
  File "/path/to/directory/get_snow_tickets.py", line 25, in get_tickets
    results = snow_talk(snow_env="prod", snow_path="api/now/table/{}?sysparm_query={}".format(snow_table, query_string))['result']
  File "/usr/local/lib/python3.6/site-packages/snow_api.py", line 18, in snow_talk
    snow_response = request.urlopen(snow_request, context = context)
  File "/usr/lib64/python3.6/urllib/request.py", line 223, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib64/python3.6/urllib/request.py", line 526, in open
    response = self._open(req, data)
  File "/usr/lib64/python3.6/urllib/request.py", line 544, in _open
    '_open', req)
  File "/usr/lib64/python3.6/urllib/request.py", line 504, in _call_chain
    result = func(*args)
  File "/usr/lib64/python3.6/urllib/request.py", line 1361, in https_open
    context=self._context, check_hostname=self._check_hostname)
  File "/usr/lib64/python3.6/urllib/request.py", line 1318, in do_open
    encode_chunked=req.has_header('Transfer-encoding'))
  File "/usr/lib64/python3.6/http/client.py", line 1254, in request
    self._send_request(method, url, body, headers, encode_chunked)
  File "/usr/lib64/python3.6/http/client.py", line 1295, in _send_request
    self.putheader(hdr, value)
  File "/usr/lib64/python3.6/http/client.py", line 1231, in putheader
    if _is_illegal_header_value(values[i]):
TypeError: expected string or bytes-like object

-->

Any help would be much appreciated!

Source: Python-3x Questions

LEAVE A COMMENT