Dogworld001 2022-02-14 11:32 采纳率: 100%
浏览 36
已结题

python树莓派运行代码区报错

python树莓派运行代码区报错

原文:https://blog.csdn.net/a497785609/article/details/78060029?utm_source=app&app_version=4.21.0&code=app_1562916241&uLinkId=usr1mkqgl919blen

问题相关代码,请勿粘贴截图

```python
#coding: utf8
import sys
import RPi.GPIO as GPIO
import time
import os
import tornado.ioloop
import tornado.web
import tornado.httpserver
import tornado.options
from tornado.options import define,options

#初始化?
def init():
    GPIO.setwarnings(False)
    GPIO.setmode(GPIO.BCM)

#设置GPIO针脚电平输出
def SetPinStatus(pin,status):
    GPIO.cleanup(int(pin))
    GPIO.setup(int(pin),GPIO.OUT)
    if(int(status)==0):
        GPIO.output(int(pin),GPIO.LOW)
    else:
        GPIO.output(int(pin),GPIO.HIGH)
    
#路由处理    
class IndexHandler(tornado.web.RequestHandler):
        def get(self):
                self.render("index.html")
        def post(self):
                init()
        action=self.get_argument('action')
        #设置GPIO针脚电平输出
        if(action=="set-gpio-pin"):
            pin = self.get_argument('pin')
                    status = self.get_argument('status')
            SetPinStatus(pin,status)
            self.write("true")
        #执行shell脚本,如:关机,重启
        elif(action=="run-shell-cmd"): 
            cmd = self.get_argument('cmd')
                    os.system(cmd)                    
                
if __name__ == '__main__':
    #控制台输出响应结果,正式环境可以不开?
    #tornado.options.parse_command_line()
    settings={
        "static_path":os.path.join(os.path.dirname(__file__),"static")
        }
    app = tornado.web.Application(
        handlers=[
            (r"/",IndexHandler),
            (r"(apple-touch-icon\.png)",tornado.web.StaticFileHandler,dict(path=settings['static_path']))
        ],**settings)
    
    http_server = tornado.httpserver.HTTPServer(app)
    http_server.listen(8020)
    tornado.ioloop.IOLoop.instance().start()
    GPIO.cleanup()
        


###### 运行结果及报错内容 
Traceback (most recent call last):
  File "/home/pi/gpio/gpio.py", line 26, in <module>
    class IndexHandler(tornado.web.RequestHandler):
  File "/home/pi/gpio/gpio.py", line 31, in IndexHandler
    action=self.get_argument('action')
NameError: name 'self' is not defined


  • 写回答

2条回答 默认 最新

  • _GX_ 2022-02-14 11:50
    关注
    class IndexHandler(tornado.web.RequestHandler):
        def get(self):
            self.render("index.html")
        def post(self):
            init()
            action=self.get_argument('action')
            #设置GPIO针脚电平输出
            if(action=="set-gpio-pin"):
                pin = self.get_argument('pin')
                        status = self.get_argument('status')
                SetPinStatus(pin,status)
                self.write("true")
            #执行shell脚本,如:关机,重启
            elif(action=="run-shell-cmd"): 
                cmd = self.get_argument('cmd')
                        os.system(cmd)            
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 2月22日
  • 已采纳回答 2月14日
  • 创建了问题 2月14日

悬赏问题

  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥15 键盘指令混乱情况下的启动盘系统重装