我在Raspberry PI上遇到python脚本问题。我有Arduino,它向我的RPi发送一个字符串(带有PHPget参数的URL地址)。收到的字符串是完整的URL地址,我只需要打开这个地址并将参数发送到我的在线数据库。字符串被正确接收,但是当我尝试通过urllib2打开它时,我收到一个错误:
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'
这是源代码的一部分,我使用:
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()
感谢您的帮助,祝您度过愉快的一天:)
所有源代码(我有GPIO控制器用于播放器的红外遥控器):
#!/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()
答案 0 :(得分:1)
根据您的追溯,您使用的url
变量用于urllib2.urlopen()
。我不确定你在哪里创建url
变量,但我很确定url
是一个列表,而不是一个字符串。
我用list
作为参数尝试了urllib2.urlopen(),我得到了同样的错误 -
>>> 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'
你应该将urllib2.urlopen()
与字符串一起使用,而不是列表,因为我不确定如何创建url
列表,我无法帮助如何从中获取字符串url,但是(看着程序)也许您打算使用joined_seq
?