1

Ok so, following code "talks" to the same restapi from live host and from pycharm editor on local venv. Every other piece of code and other tones of requests works fine here and here.
What is weird, follwoing code works from manage.py runserver launched by pycharm on local venv but does not work on live host with apache+django.

views.py

def host_tst_online(request):
    abc_hosts = Host.objects.all()
    tsthost = Host.objects.filter(abcenv='tst').order_by('abchostname')
    tsthostonline = []
    arr = []
    for xyz in tsthost:
        try:
            requests.get(
                'https://{}:6666/abc/api/v1/about'.format(xyz),
                verify='/cert/cacerts.pem',
                headers={'Accept': 'application/json', 'Authorization': 'Basic xxxxxxxxxxxxxxxxx'}, timeout=1
            )
            tsthostonline.append(xyz)
        except (ConnectionError, ConnectTimeout) as e:
            print(e)
            response = "No response"
    if request.method == 'POST':
        send_form = SendersFormO(request.POST)
        recv_form = ReceiversFormO(request.POST)
        if send_form.is_valid():
            abcsend = send_form.cleaned_data.get('abcsend')
            send_form = SendersFormO()
            for eee in tsthostonline:
                response = requests.get(
                    'https://{}:6666/abc/api/v1/objects/abcsend?id={}'.format(eee, abcsend),
                    verify='/cert/cacerts.pem',
                    headers={'Accept': 'application/json', 'Authorization': 'Basic xxxxxxxxxxxxxxxxxx'},
                ).json()
                for key in response['data']:
                    arr.append(key)
            context = {'response': response, 'arr': arr, 'send_form': send_form, 'tsthostonline': tsthostonline}
            return render(request, 'app/json_nest_send_online.html', context)
        if recv_form.is_valid():
            abcrecv = recv_form.cleaned_data.get('abcrecv')
            recv_form = ReceiversFormO()
            for eee in tsthostonline:
                response = requests.get(
                    'https://{}:6666/abc/api/v1/objects/abcrecv?id={}'.format(eee, abcrecv),
                    verify='/cert/cacerts.pem',
                    headers={'Accept': 'application/json', 'Authorization': 'Basic xxxxxxxxxxxxxxxxxxx'},
                ).json()
                for key in response['data']:
                    arr.append(key)
            context = {'response': response, 'arr': arr, 'recv_form': recv_form, 'tsthostonline': tsthostonline}
            return render(request, 'app/json_nest_recv_online.html', context)
    else:
        send_form = SendersFormO()
        recv_form = ReceiversFormO()
        context = {'response': response, 'send_form': send_form, 'recv_form': recv_form, 'abc_hosts': abc_hosts, 'tsthost': tsthost}
        return render(request, 'app/host_tst_online.html', context)


json_nest_send_online.html

[...]
    {% if response %}
        {% for key in arr %}
        <div class="border w-100"></div>
        <div style="width: 200px;">{{ key.links.self|host_cut }}</div>
        <div style="width: 100px;">{{ key.type }}</div>
        <div style="width: 150px;">{{ key.id }}</div>
        {% endfor %}
    </div>
    <hr>
{% else %}
    <p>No IDs are available.</p>
{% endif %}
[...]

How is it possible that same code, which requests to same restapi (everything with network and restapi is fine)
gives following error on live host but works when lunched from local venv:

KeyError at /app/host_tst/
'data'

i can see that arr variable is filled with response['data'] but it somehow crashes.

gipcu
  • 265
  • 2
  • 14

1 Answers1

3

This can happen if network of your hosted machine has a firewall, or is not allowed to connect to the API's server. Ideally before assuming and reading response['data'] you should first check response.status.

BadMan
  • 33
  • 5
  • nope, network and restapi is fine, this is no firewall issue. Every other piece of code, tones of requests works fine here and here – gipcu Sep 04 '19 at 15:47
  • ``200``. I can see in debug that arr variable is filled with data and it seems like with the last possible host request it crashes. – gipcu Sep 04 '19 at 15:52