我测试了我的FLASK python应用程序,当我直接从cli运行应用程序时,api工作,但是当我从apache运行它时,请求工作但是post方法根本不工作。我试过修改我的.htaccess没有任何成功。我运行命令curl http://127.0.0.1/lock -d "LockSwitch=1" -X POST -v
,我得到的错误如下。
* Hostname was NOT found in DNS cache
* Trying 127.0.0.1...
* Connected to 127.0.0.1 (127.0.0.1) port 80 (#0)
> POST /lock HTTP/1.1
> User-Agent: curl/7.38.0
> Host: 127.0.0.1
> Accept: */*
> Content-Length: 12
> Content-Type: application/x-www-form-urlencoded
>
* upload completely sent off: 12 out of 12 bytes
< HTTP/1.1 404 NOT FOUND
< Date: Sun, 11 Dec 2016 00:08:48 GMT
* Server Apache/2.4.10 (Raspbian) is not blacklisted
< Server: Apache/2.4.10 (Raspbian)
< Content-Length: 233
< Content-Type: text/html
<
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<title>404 Not Found</title>
<h1>Not Found</h1>
<p>The requested URL was not found on the server. If you entered the URL manually please check your spelling and try again.</p>
* Connection #0 to host 127.0.0.1 left intact
我在下面发布我的代码。任何帮助实现这一目标的帮助都很大。感谢
from flask import Flask, request
from flask_restful import reqparse, abort, Api, Resource
from sqlalchemy import create_engine
from json import dumps
import serial
from time import sleep
serialport = serial.Serial("/dev/ttyAMA0", 115200, timeout=0.5)
#Create a engine for connecting to SQLite3.
#Assuming salaries.db is in your app root folder
e = create_engine('sqlite:///lockdb.db')
app = Flask(__name__)
api = Api(app)
def abort_if_todo_doesnt_exist(todo_id):
if todo_id not in TODOS:
abort(404, message="Todo {} doesn't exist".format(todo_id))
parser = reqparse.RequestParser()
parser.add_argument('LockSwitch')
class Keystat_Meta(Resource):
def get(self):
#Connect to databse
conn = e.connect()
#Perform query and return JSON data
query = conn.execute("select * from lockstat")
return {'KeyStat': [i[0] for i in query.cursor.fetchall()]}
class lock(Resource):
def get(self, slot_number):
serialport.write(slot_number.upper())
sleep(0.004)
serialport.write('relay')
return ("lock_shift")
class LockSwitcher(Resource):
def post(self):
args = parser.parse_args()
serialport.write(args['LockSwitch'])
sleep(0.004)
serialport.write('relay')
LockSwitcheroo = {'LockSwitch': args['LockSwitch']}
return LockSwitcheroo, 201
api.add_resource(Keystat_Meta, '/keystat')
api.add_resource(LockSwitcher, '/lock')
api.add_resource(lock, '/lock/<string:slot_number>')
if __name__ == '__main__':
app.run(debug=True)
以下是我的wsgi
from slot import app as application
我的.htaccess
<Location />
AllowMethods GET POST OPTIONS
Allow from all
</Location>
最后但并非最不重要的是我的虚拟主机conf
<VirtualHost *:80>
ServerName www.mytestdomain.com
ServerAdmin webmaster@localhost
DocumentRoot /var/www/slot
WSGIDaemonProcess slot threads=5
WSGIScriptAlias / /var/www/slot/slot.wsgi
<Directory /var/www/slot>
WSGIProcessGroup slot
WSGIApplicationGroup %{GLOBAL}
Require all granted
</Directory>
</VirtualHost>
# vim: syntax=apache ts=4 sw=4 sts=4 sr noet