HTTP GET request in Python

Asked by At

I really need some help. I'm new in python and programming.

I am trying to run an experiment in Open Sesame and get some data from a Neulog device. Neulog provides an API to interact with the device. The API is based on the HTTP protocol so my question is:

How does the python code needs to look contact the NeuLog API via the http “GET” command?

2 Answers

Tim On

I take it from your question you are trying to make an HTTP GET request?

Take a look at the requests package. This makes performing HTTP requests easy.

Ellisein On

You can make HTTP request in several ways, but i recommend using requests(blocking) or aiohttp(async).

With requests,

import requests

def get(url):
    response = requests.get(url)
    if response.status_code == 200:
        return response.text
        return None

response = get("some_url")

With asyncio and aiohttp,

import asyncio
import aiohttp

async def get(url):
    async with aiohttp.ClientSession() as session:
        async with session.get(url) as response:
            if response.status == 200:
                return await response.text()
                return None

response = get("some url")