Jump to content


Problem with the urllib library in python

raspberry urllib

  • Please log in to reply
1 reply to this topic

#1 yukardo



  • Members
  • Pip
  • 2 posts

Posted 30 April 2017 - 10:28 PM



I am using the tutorial for communicating a raspberry Pi with Carriots.


This is the code:


    Created 11 Jan 2013
    This sketch sends streams to Carriots according to the values read by a LDR sensor
import RPi.GPIO as GPIO
from urllib2 import urlopen, Request
from time import mktime, sleep
from datetime import datetime
from json import dumps
class Client (object):
    def __init__(self, api_key=None, client_type='json'):
        self.client_type = client_type
        self.api_key = api_key
        self.content_type = "application/vnd.carriots.api.v2+%s" % self.client_type
        self.headers = {'User-Agent': 'Raspberry-Carriots',
                        'Content-Type': self.content_type,
                        'Accept': self.content_type,
                        'Carriots.apikey': self.api_key}
        self.data = None
        self.response = None
    def send(self, data):
        self.data = dumps(data)
        request = Request(Client.api_url, self.data, self.headers)
        self.response = urlopen(request)
        return self.response
def rc_time(pipin):
    measurement = 0
    GPIO.setup(pipin, GPIO.OUT)
    GPIO.output(pipin, GPIO.LOW)
    GPIO.setup(pipin, GPIO.IN)
    while GPIO.input(pipin) == GPIO.LOW:
        measurement += 1
    return measurement
def main():
    on = 1  # Constant to indicate that lights are on
    off = 2  # Constant to indicate that lights are off
    device = "YOUR DEVICE's ID_DEVELOPER HERE"  # Replace with the id_developer of your device
    apikey = "YOUR APIKEY HERE"  # Replace with your Carriots apikey
    lights = off  # Current status
    client_carriots = Client(apikey)
    # The loop routine runs over and  over again forever
    while True:
        timestamp = int(mktime(datetime.utcnow().timetuple()))
        print rc_time(4)
        # This is the value limit between day or night with or LDR sensor and our capacitor.
        # Maybe you need adjust this value.
        if rc_time(4) > 600:
            new_lights = off
            print("Lights OFF")
            new_lights = on
            print("Lights ON")
        if lights is not new_lights:  # Check if we have a change in status
            lights = new_lights  # Status update and send stream
            data = {"protocol": "v2", "device": device, "at": timestamp, "data": dict(
                light=("ON" if new_lights is on else "OFF"))}
            carriots_response = client_carriots.send(data)
            print carriots_response.read()
if __name__ == '__main__':
I am using python 3 and I have problem with the urllib library.
I hope you can help me.

#2 sdeancos


    Master of Puppets

  • Members
  • PipPipPip
  • 108 posts
  • LocationMadrid, Spain.

Posted 02 May 2017 - 03:13 PM


You should change this line (python 2.7.x):
from urllib2 import urlopen, Request

for (python 3.x):
from urllib.request import urlopen, Request



I hope this help you.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users