0

I have a problem with the urllib library in Python 3.

This is my code:

!/usr/bin/python3
import http.client
import urllib.request
import urllib.parse
import time
from datetime import datetime
import json


device = "[email protected]"  
api_key = ""
api_url = "http://api.carriots.com/devices"



#Parameters - Body
timestamp = int(time.time())
params = {"order": -1}
binary_data = json.dumps(params).encode('ascii')

#Header
header = { "User-Agent": "raspberrycarriots",
       "Content-Type": "application/json",
       "carriots.apikey": api_key}

#Request
req= urllib.request.Request(api_url, binary_data, header)
print(req)
f = urllib.request.urlopen(req)

#Print response
print(f.read().decode('utf-8'))

This is the answer that I got:

urllib.request.Request object at 0x74cd2370>
Traceback (most recent call last):
File "/usr/lib/python3.4/urllib/request.py", line 1174, in do_open
h.request(req.get_method(), req.selector, req.data, headers)
File "/usr/lib/python3.4/http/client.py", line 1090, in request
self._send_request(method, url, body, headers)
File "/usr/lib/python3.4/http/client.py", line 1128, in _send_request
self.endheaders(body)
File "/usr/lib/python3.4/http/client.py", line 1086, in endheaders
self._send_output(message_body)
File "/usr/lib/python3.4/http/client.py", line 924, in _send_output
self.send(msg)
File "/usr/lib/python3.4/http/client.py", line 859, in send
self.connect()
File "/usr/lib/python3.4/http/client.py", line 836, in connect
self.timeout, self.source_address)
File "/usr/lib/python3.4/socket.py", line 491, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
File "/usr/lib/python3.4/socket.py", line 530, 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 "/home/pi/Documents/Python Programs/demoget.py", line 31, in <module>
    f = urllib.request.urlopen(req)
  File "/usr/lib/python3.4/urllib/request.py", line 153, in urlopen
    return opener.open(url, data, timeout)
  File "/usr/lib/python3.4/urllib/request.py", line 455, in open
    response = self._open(req, data)
  File "/usr/lib/python3.4/urllib/request.py", line 473, in _open
    '_open', req)
  File "/usr/lib/python3.4/urllib/request.py", line 433, in _call_chain
    result = func(*args)
  File "/usr/lib/python3.4/urllib/request.py", line 1202, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/usr/lib/python3.4/urllib/request.py", line 1176, in do_open
    raise URLError(err)
urllib.error.URLError: <urlopen error [Errno -2] Name or service not known>

I hope you can help me.

1 Answer 1

1

Your program still works today with that same API key. The output is:

<urllib.request.Request object at 0x104a77940>
{"total_documents":2,"result":[{"_id":"590327165c5d7564440e39f7","n_str":1494722195,"updated_at":1494722209,"type":"Raspberry Pi","owner":"yukardo","id_group":"[email protected]","id_model":null,"lon":null,"id_asset":null,"frequency_stream":1440,"sensor":"Other","before_status":"ok","status":"disconnected","description":null,"id_developer":"[email protected]","frequency_status":1440,"lat":null,"properties":[],"name":"Miriadax","checksum":null,"created_at":1493378838,"enabled":true,"time_zone":"America\/Santo_Domingo","updater":"system","_ls":{"_id":"5907c3c45c5d75f37222897b","id_d":"[email protected]","l_str":1494635795,"n_str":1494722195,"d":1,"owner":"system","updater":"system"}},{"_id":"592988b05c5d758d3d0c7f92","n_str":1500600100,"updated_at":1500600129,"type":"Other","owner":"yukardo","id_group":"[email protected]","id_model":null,"lon":null,"id_asset":null,"frequency_stream":1440,"sensor":"Other","before_status":"ok","status":"disconnected","description":null,"id_developer":"[email protected]","frequency_status":1440,"lat":null,"properties":[],"name":"WallE","checksum":null,"created_at":1495894192,"enabled":true,"time_zone":"America\/Santo_Domingo","updater":"system","_ls":{"_id":"592e1d925c5d755b1aa852b4","id_d":"[email protected]","l_str":1500513700,"n_str":1500600100,"d":1,"owner":"system","updater":"system"}}]}

(The first # byte of your program appears to be cut off, but I'll assume that was a copypaste accident.)

However, if api.carriots.com were unreachable you would get exactly the Name or service not known error as you saw. Run ping api.carriots.com, check your Pi's ethernet/WiFi configuration as necessary, and retry once networking issues are resolved.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.