Pythonスクリプトはsystemdからネットワーク要求を実行できず、コマンドラインから実行できます。

Pythonスクリプトはsystemdからネットワーク要求を実行できず、コマンドラインから実行できます。

HTTPリクエストを受信し、詳細に応じて着信リクエストを処理するプロセスの一環として、独自のHTTPリクエストを生成できるPythonで書かれた単純なサーバーがあります。私のサーバーが実行する要求は複数層の抽象化ですが、最終的にはrequests通常の古いライブラリを使用して行われます。

これは、コマンドラインからサーバーを実行すると効果的です。これでシステムサービスを作成しようとしましたが、HTTP要求を生成できなくなりました。他のすべての機能は正常に動作しているように見えますが、要求は失敗し、大きくて見苦しいエラーが発生します。

私の.serviceファイルと失敗したHTTPリクエストログが含まれていました。ホスト名とスクリプトパスは明らかに変更されていますが、関連するすべては同じままです。これは一般的な要求なので、コードの詳細は関連性がないはずだと思いますrequestsが、必要に応じて詳細を提供できます。このような要請が失敗した理由についての洞察力があれば、よろしくお願いします。

フレームワークサーバー。サービス:

[Unit]
Description=Run server for testing framework

[Service]
WorkingDirectory=/path/to/my/script
Environment=PYTHONUNBUFFERED=1
User=username
Group=username
Restart=on-failure
RestartSec=5
ExecStart=/usr/bin/env python3 server.py
SyslogIdentifier=server.py

[Install]
WantedBy=multi-user.target

GETリクエストを送信しようとしたときのエラーログmy.request.url/index.php?/api/v2/get_case/4887104

Exception happened during processing of request from ('10.40.200.106', 56051)
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 144, in _new_conn
    (self.host, self.port), self.timeout, **extra_kw)
  File "/usr/lib/python3/dist-packages/urllib3/util/connection.py", line 60, in create_connection
    for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  File "/usr/lib/python3.6/socket.py", line 745, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 601, in urlopen
    chunked=chunked)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 346, in _make_request
    self._validate_conn(conn)
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 852, in _validate_conn
    conn.connect()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 298, in connect
    conn = self._new_conn()
  File "/usr/lib/python3/dist-packages/urllib3/connection.py", line 153, in _new_conn
    self, "Failed to establish a new connection: %s" % e)
urllib3.exceptions.NewConnectionError: <urllib3.connection.VerifiedHTTPSConnection object at 0x7fb3bcd36208>: Failed to establish a new connection: [Errno -2] Name or service not known
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 440, in send
    timeout=timeout
  File "/usr/lib/python3/dist-packages/urllib3/connectionpool.py", line 639, in urlopen
    _stacktrace=sys.exc_info()[2])
  File "/usr/lib/python3/dist-packages/urllib3/util/retry.py", line 398, in increment
    raise MaxRetryError(_pool, url, error or ResponseError(cause))
urllib3.exceptions.MaxRetryError: HTTPSConnectionPool(host='my.request.url', port=443): Max retries exceeded with url: /index.php?/api/v2/get_case/4887104 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fb3bcd36208>: Failed to establish a new connection: [Errno -2] Name or service not known',))
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
  File "/usr/lib/python3.6/socketserver.py", line 320, in _handle_request_noblock
    self.process_request(request, client_address)
  File "/usr/lib/python3.6/socketserver.py", line 351, in process_request
    self.finish_request(request, client_address)
  File "/usr/lib/python3.6/socketserver.py", line 364, in finish_request
    self.RequestHandlerClass(request, client_address, self)
  File "/usr/lib/python3.6/socketserver.py", line 724, in __init__
    self.handle()
  File "/usr/lib/python3.6/http/server.py", line 418, in handle
    self.handle_one_request()
  File "/usr/lib/python3.6/http/server.py", line 406, in handle_one_request
    method()
  File "server.py", line 194, in do_POST
    test_handler.run_tests(run_id, project_id, suite_id, case_id, test_ids)
  File "/path/to/my/script/lib/testing_automation/testrail_integration/test_handler.py", line 70, in run_tests
    my_case = client.get_case(case_id)
  File "/path/to/my/script/lib/testing_automation/testrail_integration/testrail_wrapper.py", line 68, in get_case
    return super().send_get(f'get_case/{case_id}')
  File "/path/to/my/script/lib/testing_automation/testrail_integration/testrail.py", line 41, in send_get
    return self.__send_request('GET', uri, filepath)
  File "/path/to/my/script/lib/testing_automation/testrail_integration/testrail_wrapper.py", line 48, in _APIClient__send_request
    return super()._APIClient__send_request(method, uri, data)
  File "/path/to/my/script/lib/testing_automation/testrail_integration/testrail.py", line 79, in __send_request
    response = requests.get(url, headers=headers)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 72, in get
    return request('get', url, params=params, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/api.py", line 58, in request
    return session.request(method=method, url=url, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 520, in request
    resp = self.send(prep, **send_kwargs)
  File "/usr/lib/python3/dist-packages/requests/sessions.py", line 630, in send
    r = adapter.send(request, **kwargs)
  File "/usr/lib/python3/dist-packages/requests/adapters.py", line 508, in send
    raise ConnectionError(e, request=request)
requests.exceptions.ConnectionError: HTTPSConnectionPool(host='my.request.url', port=443): Max retries exceeded with url: /index.php?/api/v2/get_case/4887104 (Caused by NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7fb3bcd36208>: Failed to establish a new connection: [Errno -2] Name or service not known',))

ベストアンサー1

Jeff Schallerはコメントで問題を確認しました。要求を行うために必要なプロキシがCLI環境に設定されているが、あまりにも前に設定されているため、必要であることを忘れていました。

Environment=https_proxy="http://location.of.proxy".serviceファイルを含む問題を修正しました。

おすすめ記事