【问题标题】:Python and converting string into valid URLPython并将字符串转换为有效的URL
【发布时间】:2015-07-12 15:23:51
【问题描述】:

我的 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()

【问题讨论】:

  • 你在哪里创建 url 变量?
  • 对不起,我的错。必须有 join_seq 而不是 url。对不起。

标签: python


【解决方案1】:

根据您的回溯,您将 url 变量用于 urllib2.urlopen() 。我不确定你在哪里创建 url 变量,但我很确定 url 是一个列表,而不是一个字符串。

我尝试了 urllib2.urlopen() 与 list 作为参数,我得到了同样的错误 -

>>> 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?

【讨论】:

  • 对不起,我的错。必须有 join_seq 而不是 url。对不起。
  • 那之后有效果吗?还是您遇到同样的错误?
  • 能否请您用最新的回溯更新问题,回溯仍然显示您正在使用url 变量,您可以再试一次并更新回溯吗?
  • 在启动时,它只写 [] 。会不会是这个问题?在启动脚本只接收括号 [] 并且脚本崩溃?因为,当我删除命令 urllib.urlopen... 时,我可以正确看到接收到的数据
  • 因为ser.read()里面什么都没有,能多显示代码吗?
猜你喜欢
  • 2013-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-03-08
  • 1970-01-01
  • 2015-01-19
  • 2021-12-01
  • 1970-01-01
相关资源
最近更新 更多