Pavol Bystrican
Pavol Bystrican

Reputation: 11

Python and converting string into valid URL

I have a trouble with python script on my Raspberry PI. I have Arduino, which sends a string (URL address with parameters for PHPget) to my RPi. Received string is full URL address, I only need to open this address and send parameters to my online database. String is received correctly, but when I try to open this by urllib2, I get an error:

Traceback (most recent call last):
   File "blesk.py", line 102, in <module>
     response = urllib2.urlopen(joined_seq)
   File "/usr/lib/python2.7/urllib2.py", line 127, in urlopen
     return _opener.open(url, data, timeout)
   File "/usr/lib/python2.7/urllib2.py", line 392, in open
     req.timeout = timeout
AttributeError: 'list' object has no attribute 'timeout'

Here is a part of source code, which I use:

  for c in ser.read():
        seq.append(c)
        joined_seq = ''.join(str(v) for v in seq)

        if c == '\n':
            loop_value = 1
            break

   while (loop_value == 1):
        try:
                urllib2.urlopen("http://www.blesky.pablox.net/spojenie.php")
        except urllib2.URLError, e:
                print("connection error")
                time.sleep(1)
        else:
                print("connection OK")
                print joined_seq
                response = urllib2.urlopen(joined_seq)
                loop_value=0
ser.close()

Thanks for your help and have a nice day :)

All source code (I have there GPIO control for IR remote control of player):

#!/usr/bin/env python

import telnetlib;
import time;
import os;
import RPi.GPIO as GPIO
import subprocess;
import serial;
import datetime;
import urllib2;
from time import sleep
import urllib


GPIO.setmode(GPIO.BCM)
GPIO.setup(2, GPIO.IN)
GPIO.setup(3, GPIO.IN)
GPIO.setup(4, GPIO.IN)
GPIO.setup(14, GPIO.IN)

#define serial port
ser = serial.Serial(
    port='/dev/ttyUSB0',\
    baudrate=9600,\
    parity=serial.PARITY_NONE,\
    stopbits=serial.STOPBITS_ONE,\
    bytesize=serial.EIGHTBITS,\
        timeout=0)

print("connected to: " + ser.portstr)

#this will store the line
line = []
seq = []
joined_seq = []
# URL 
url = []
loop_value = 1
# MPC INIT
os.system ("mpc clear")
os.system("mpc load radia")
time.sleep(2)
os.system("mpc play 1")

# IR REMOTE CONTROL
var = 1;
while var == 1 :


   if ( GPIO.input(2) == 0 )&( GPIO.input(3) == 1 )&( GPIO.input(4) == 1 )&( GPIO.input(14) == 1 ):
        os.system('mpc toggle')
        sleep(0.1)
# Volny kanal pre tlacidlo CM
#       if ( GPIO.input(2) == 1 )&( GPIO.input(3) == 0 )&( GPIO.input(4) == 1 )&( GPIO.input(14) == 1 ):
   if ( GPIO.input(2) == 0 )&( GPIO.input(3) == 0 )&( GPIO.input(4) == 1 )&( GPIO.input(14) == 1 ):
        os.system('mpc volume +2')
        sleep(0.1)
   if ( GPIO.input(2) == 1 )&( GPIO.input(3) == 1 )&( GPIO.input(4) == 0 )&( GPIO.input(14) == 1 ):
        os.system('mpc volume -2')
        sleep(0.1)
   if ( GPIO.input(2) == 0 )&( GPIO.input(3) == 1 )&( GPIO.input(4) == 0 )&( GPIO.input(14) == 1 ):
        os.system('mpc play 1')
   if ( GPIO.input(2) == 0 )&( GPIO.input(3) == 0 )&( GPIO.input(4) == 0 )&( GPIO.input(14) == 1 ):
        os.system('mpc play 2')
   if ( GPIO.input(2) == 1 )&( GPIO.input(3) == 1 )&( GPIO.input(4) == 1 )&( GPIO.input(14) == 0 ):
        os.system('mpc play 3')
   if ( GPIO.input(2) == 0 )&( GPIO.input(3) == 1 )&( GPIO.input(4) == 1 )&( GPIO.input(14) == 0 ):
        os.system('mpc play 4')
   if ( GPIO.input(2) == 1 )&( GPIO.input(3) == 0 )&( GPIO.input(4) == 1 )&( GPIO.input(14) == 0 ):
        os.system('mpc play 5')
   if ( GPIO.input(2) == 0 )&( GPIO.input(3) == 0 )&( GPIO.input(4) == 1 )&( GPIO.input(14) == 0 ):
        os.system('mpc play 6')
   if ( GPIO.input(2) == 1 )&( GPIO.input(3) == 1 )&( GPIO.input(4) == 0 )&( GPIO.input(14) == 0 ):
        os.system('mpc play 7')
   if ( GPIO.input(2) == 0 )&( GPIO.input(3) == 1 )&( GPIO.input(4) == 0 )&( GPIO.input(14) == 0 ):
        os.system('mpc play 8')
   if ( GPIO.input(2) == 1 )&( GPIO.input(3) == 0 )&( GPIO.input(4) == 0 )&( GPIO.input(14) == 0 ):
        os.system('mpc play 9')

# HERE STARTS PART OF UART AND PHPGET   
   for c in ser.read():
        joined_seq = ""
        seq.append(c)
        joined_seq = ''.join(str(v) for v in seq)

        if c == '\n':
            loop_value = 1
            break

   while (loop_value == 1):
        try:
                urllib2.urlopen("http://www.blesky.pablox.net/spojenie.php")
        except urllib2.URLError, e:
                print("chyba spojenia")
                time.sleep(1)
        else:
                print("connection OK")
                print joined_seq
                sleep(3)
                response = urllib2.urlopen(joined_seq)
                loop_value=0
ser.close()

Upvotes: 1

Views: 1100

Answers (1)

Anand S Kumar
Anand S Kumar

Reputation: 90999

According to your traceback, you are using url variable for the urllib2.urlopen() . I am not sure where you are creating url variable, but I am pretty sure url is a list, not a string.

I tried urllib2.urlopen() with list as an argument, and i got the same error -

>>> import urllib2
>>> urllib2.urlopen(['http://www.google.com'])
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/lib64/python2.6/urllib2.py", line 126, in urlopen
    return _opener.open(url, data, timeout)
  File "/usr/lib64/python2.6/urllib2.py", line 382, in open
    req.timeout = timeout
AttributeError: 'list' object has no attribute 'timeout'

You should use urllib2.urlopen() with string, not a list , since I am not sure how url list is created, I cannot help on how to get the string url from it, but (looking at the program) maybe you intended to use joined_seq ?

Upvotes: 1

Related Questions