我目前有两个连接到我的Raspberry Pi的按钮(这些是带有环形LED的那些)并且我正在尝试执行此代码
#!/usr/bin/env python
import RPi.GPIO as GPIO
import time
GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(17, GPIO.OUT) #green LED
GPIO.setup(18, GPIO.OUT) #red LED
GPIO.setup(4, GPIO.IN, GPIO.PUD_UP) #green button
GPIO.setup(27, GPIO.IN, GPIO.PUD_UP) #red button
def remove_events():
GPIO.remove_event_detect(4)
GPIO.remove_event_detect(27)
def add_events():
GPIO.add_event_detect(4, GPIO.FALLING, callback=green, bouncetime=800)
GPIO.add_event_detect(27, GPIO.FALLING, callback=red, bouncetime=800)
def red(pin):
remove_events()
GPIO.output(17, GPIO.LOW)
print "red pushed"
time.sleep(2)
GPIO.output(17, GPIO.HIGH)
add_events()
def green(pin):
remove_events()
GPIO.output(18, GPIO.LOW)
print "green pushed"
time.sleep(2)
GPIO.output(18, GPIO.HIGH)
add_events()
def main():
while True:
print "waiting"
time.sleep(0.5)
GPIO.output(17, GPIO.HIGH)
GPIO.output(18, GPIO.HIGH)
GPIO.add_event_detect(4, …Run Code Online (Sandbox Code Playgroud) 我试图让我的 MQTT Paho Python 脚本保持连接(并在断开连接时重新连接)。可悲的是,我不知道该怎么做。
也就是说,机器是通过 WiFi 连接的,所以在信号丢失或 USB 加密狗被卡住的情况下,我不希望代码对我不利,所以我试图涵盖所有异常/错误。
这是我正在尝试使用的一段代码:
mqttc = mqtt.Client(machine_id, clean_session=False)
mqttc.username_pw_set(machine_id, mqtt_pwd)
mqttc.connect(mqtt_host, mqtt_port)
mqttc.subscribe(machine_id, qos=1)
def on_disconnect(client, userdata, rc):
if rc != 0:
print "Unexpected MQTT disconnection. Attempting to reconnect."
try:
mqttc.reconnect()
except socket.error:
??????
mqttc.on_connect = on_connect
mqttc.on_message = on_message
mqttc.on_disconnect = on_disconnect
mqttc.loop_forever()
Run Code Online (Sandbox Code Playgroud)
我无法更进一步,因为我不知道如何让它再次连接?除非它能够第一次重新连接,否则我似乎无法进行正确的重新连接循环。
任何建议都会非常有帮助!
谢谢!
我目前正在获得一个非常长的JSON,我试图通过Python 2.7从它中挑选出两条信息.
JSON看起来大致如下:
{
'device': [
{
'serial': '00000000762c1d3c',
'registered_id': '019'
},
{
'serial': '000000003ad192f2',
'registered_id': '045'
},
{
'serial': '000000004c9898aa',
'registered_id': '027'
}
],
}
Run Code Online (Sandbox Code Playgroud)
在这个JSON中,我正在寻找可能与JSON中的一个匹配的特定序列.如果是,它也应该打印出registered_id.
我尝试过使用一个简单的脚本,即使没有registered_id,但我无处可去:
if '00000000762c1d3c' not in data['device']:
print 'not there'
else:
print 'there'
Run Code Online (Sandbox Code Playgroud)
谢谢你的建议!
我一直在拼命尝试让我的MQTT客户端连接到使用来自CA的证书设置的MQTT代理。(Letsencrypt:https ://pypi.python.org/pypi/letsencrypt/0.4.1 )我对我的https站点使用了相同的证书,这似乎很好用。我不确定是否可以保持任何连接。
我已使用本指南为代理设置证书(http://mosquitto.org/2015/12/using-lets-encrypt-certificates-with-mosquitto/)
代理v1.4.8可以在以下配置下正常工作:
cafile chain.pem
certfile cert.pem
keyfile privkey.pem
[ ok ] mosquitto is running.
Run Code Online (Sandbox Code Playgroud)
尝试使用调试消息连接到此代理的客户端会产生:
Client mosqsub/42074-titan sending CONNECT
Run Code Online (Sandbox Code Playgroud)
在我的经纪人方面,我收到以下错误消息:
1457358950: New connection from NOT.TELLING.YOU.OBVIOUSLY on port 8883.
1457358950: OpenSSL Error: error:140760FC:SSL routines:SSL23_GET_CLIENT_HELLO:unknown protocol
1457358950: Socket error on client <unknown>, disconnecting.
Run Code Online (Sandbox Code Playgroud)
我在高空搜寻了一个解决方案,可悲的是几乎没有。
任何帮助将不胜感激!谢谢!
我目前正在尝试使用名为Mosquitto PHP(https://github.com/mgdm/Mosquitto-PHP/)的PHP插件。我已经正确安装了所有程序,并且在“ php -m”下似乎可以正确显示。
我正在使用一个小的测试代码来查看它是否以其基本形式起作用:
<?php
$c = new Mosquitto\Client;
$c->onConnect(function() use ($c) {
$c->publish('mgdm/test', 'Hello', 2);
});
$c->connect('test.mosquitto.org');
for ($i = 0; $i < 100; $i++) {
// Loop around to permit the library to do its work
$c->loop(1);
}
echo "Finished\n";
?>
Run Code Online (Sandbox Code Playgroud)
而且似乎在我的浏览器中返回“完成”。因此,我决定升级游戏,并向其添加TLS连接(如所记录):
<?php
$c = new Mosquitto\Client;
$c->onConnect(function() use ($c) {
$c->publish('mgdm/test', 'Hello', 2);
});
$c->setTlsCertificates('mosquitto.org.crt');
$c->connect('test.mosquitto.org', '8883');
for ($i = 0; $i < 100; $i++) {
$c->loop(1);
}
echo "Finished\n";
?>
Run Code Online (Sandbox Code Playgroud)
我获得了证书,并确保apache2可以读取它,并随后将所有权设置为apache2。原来,这给了我浏览器500内部服务器错误。
-rwsrwsrwt 1 …Run Code Online (Sandbox Code Playgroud) 我的Python脚本通过os.system调用shell命令:
os.system('sudo ifdown wlan0 &> /dev/null')
Run Code Online (Sandbox Code Playgroud)
如果我在没有Python的情况下运行此命令,则在Python中输出被抑制,但是,它仍会打印输出.
我究竟做错了什么?
我有一个Raspberry Pi Camera v2.1版本,能够以3280x2464的分辨率拍照。
我已经使用raspistill命令进行了测试,这似乎可以正常工作:
raspistill -o 8mp.png -w 3280 -h 2464
Run Code Online (Sandbox Code Playgroud)
返回信息:
8mp.png JPEG 3280x2464 3280x2464+0+0 8-bit sRGB 4.524MB 0.010u 0:00.010
Run Code Online (Sandbox Code Playgroud)
但是,当我使用Python代码拍照时,它会拒绝它。这是我正在使用的代码:
#!/usr/bin/python
import picamera
camera = picamera.PiCamera()
camera.resolution = (3280,2464)
camera.capture("test.png")
camera.close()
Run Code Online (Sandbox Code Playgroud)
这是错误:
mmal: mmal_vc_port_enable: failed to enable port vc.ril.image_encode:out:0(PNG ): ENOSPC
mmal: mmal_port_enable: failed to enable port vc.ril.image_encode:out:0(PNG )(0x700090) (ENOSPC)
Traceback (most recent call last):
File "pic.py", line 6, in <module>
camera.capture("test.png")
File "/usr/local/lib/python2.7/dist-packages/picamera/camera.py", line 1383, in capture
encoder.start(output)
File "/usr/local/lib/python2.7/dist-packages/picamera/encoders.py", line 1024, in start
super(PiCookedOneImageEncoder, self).start(output) …Run Code Online (Sandbox Code Playgroud) 为了简化我遇到的情况:我试图终止一个仍在 Python 2.7 中运行的线程,但我不知道该怎么做。
拿这个简单的代码:
import time
import threading
def thread1():
print "Starting thread 1"
while True:
time.sleep(0.5)
print "Working"
thread1 = threading.Thread(target=thread1, args=())
thread1.start()
time.sleep(2)
print "Killing thread 1"
thread2.stop()
print "Checking if it worked:"
print "Thread is: " + str(thread1.isAlive())
Run Code Online (Sandbox Code Playgroud)
线程 1 继续“工作”,我试图在主线程中杀死它。关于如何做到这一点的任何想法?我试过了:
threat1.terminate
threat1.stop
threat1.quit
threat1.end
Run Code Online (Sandbox Code Playgroud)
这一切似乎都表明,没有办法用一行简单的代码来真正阻止它。你有什么建议?
我正在尝试在Python(v2.7)中编码一个有点大的JSON,而我在输入变量时遇到了麻烦!
由于JSON是多行的并且为了保持我的代码整洁,我决定使用三重双引号使其看起来如下:
my_json = """{
"settings": {
"serial": "1",
"status": "2",
"ersion": "3"
},
"config": {
"active": "4",
"version": "5"
}
}"""
Run Code Online (Sandbox Code Playgroud)
要对此进行编码,输出它对我来说效果很好,但我不知道如何更改我在那里的数字并用变量字符串替换它们.我试过了:
"settings": {
"serial": 'json_serial',
Run Code Online (Sandbox Code Playgroud)
但无济于事.任何帮助,将不胜感激!
我知道我实际上可以合并两个列表(在 Python 2.7 中)如下
list1 = ['one', 'two', 'three', 'four', 'five']
list2 = ['A', 'B', 'C', 'D', 'E']
merged = list1 + list2
print merged
# ['one', 'two', 'three', 'four', 'five', 'A', 'B', 'C', 'D', 'E']
Run Code Online (Sandbox Code Playgroud)
问题是,我希望在 list1 的每两个之后插入一个 list2。例子:
list1 = ['one', 'two', 'three', 'four', 'five']
list2 = ['A', 'B', 'C', 'D', 'E']
after 2 of list1:
add 1 of list2
print merged
# ['one', 'two', 'A', 'three', 'four', 'B', 'five', 'six', 'C', 'seven', 'eight', 'D', 'nine', 'ten']
Run Code Online (Sandbox Code Playgroud)
任何帮助将非常感激!
我正在寻找一个Python脚本,它将获得最后2或3个特定字符.更具体地说,我希望将IP名称剪切为最后2位或3位数.
import socket
ip = socket.gethostbyname(socket.gethostname())[-3:0]
Run Code Online (Sandbox Code Playgroud)
我想只是切片的IP地址并得到最后3位数,但想到当IP地址以2位数结束时(即192.168.1.XX)会出现问题
任何帮助将非常感激!
我刚刚开始使用多线程,所以我想我会做一个小而简单的例子:
import time
import threading
def count(who):
count = 1
while count <= 5:
print who + " counted to: " + str(count)
time.sleep(0.1)
count += 1
thread1 = threading.Thread(target=count, args=('i'))
thread1.start()
Run Code Online (Sandbox Code Playgroud)
哪个效果很好并打印出以下内容:
>>> i counted to: 1
>>> i counted to: 2
>>> i counted to: 3
>>> i counted to: 4
>>> i counted to: 5
Run Code Online (Sandbox Code Playgroud)
然而,奇怪的是,当我想将参数更改为另一个时,例如:"john":
thread1 = threading.Thread(target = count,args =('john'))
希望它会产生:
>>> john counted to: 1
>>> john counted to: 2
>>> john counted to: …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用Paramiko的库http://docs.paramiko.org/en/2.1/api/sftp.html与Python(v2.7)建立SFTP连接
通过网上的零碎零碎,我能够(无论如何也是如此)使用私有加密证书进行连接。
如下所示:
import paramiko
sftp = paramiko.SSHClient()
hostname = "sftp.host.com"
port = 8022
username = "sftplogin"
k = paramiko.RSAKey.from_private_key_file("private.pem", password="XXX")
sftp.set_missing_host_key_policy(paramiko.AutoAddPolicy())
print "Connecting..."
sftp.connect(hostname, port, username=username, pkey=k )
print "Connected to: " + hostname + ":" + port
print sftp.getcwd()
sftp.close()
Run Code Online (Sandbox Code Playgroud)
在这一点上,我只是想看看我是否没有妄想,并且实际上没有连接,所以我试图打印一个getcwd()来获取当前目录……可惜的是我什么都没有,因为它返回此错误:
AttributeError:“ SSHClient”对象没有属性“ getcwd”
谁能告诉我为什么?我究竟做错了什么?
提前致谢!