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日

悬赏问题

  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料