让我从这开始,我不知道python,我可能有一天通过python教程。情况就是这样。我有一个角度应用程序,在iframe的vm上有一个用Apache托管的python应用程序。我没有编写python应用程序,但是另一个开发人员给我写了一个端点,我应该可以从我的角度应用程序发布。
制作python端点的开发人员说我的请求有问题,但我相当确定没有任何错误。我几乎100%确定问题是响应中没有CORS头和/或响应未设置为响应OPTIONS方法。下面是整个python端点:
import os, site, inspect
site.addsitedir(os.path.dirname(os.path.abspath(inspect.getfile(inspect.currentframe())))+"/../")
import json
from datetime import datetime
import pymongo
from Config import Config
def application(environ, start_response):
response = environ['wsgi.input'].read(int(environ['CONTENT_LENGTH']))
if response:
json_response = json.loads(response)
document = {
'payment_id': json_response['payment_id'],
'log': json_response['log'],
'login_id': json_response['login_id'],
'browser': environ.get('HTTP_USER_AGENT', None),
'ip_address': environ.get('REMOTE_ADDR', None),
'created_at': datetime.utcnow(),
}
client = pymongo.MongoClient(Config.getValue('MongoServer'))
db = client.updatepromise
db.PaymentLogs.insert(document)
start_response('200 OK', [('Content-Type', 'application/json')
return '{"success": true}'
start_response('400 Bad Request', [('Content-Type', 'application/json')])
return '{"success": false}'
我尝试了以下方法来完成这项工作:我在start_response函数中添加了更多标题,所以代码现在看起来像这样:
start_response('201 OK', [('Content-Type', 'application/json',
('Access-Control-Allow-Headers','authorization'),
('Access-Control-Allow-Methods','HEAD, GET, POST, PUT, PATCH, DELETE'),
('Access-Control-Allow-Origin','*'),
('Access-Control-Max-Age','600'))])
不是:我一开始就用200和400的响应做了这个,并且在响应中看不到任何变化,然后只是为了它,我决定将200更改为201,这也是没有通过回复,所以我怀疑这段代码甚至没有因为某些原因而被运行。
请帮忙,python newb在这里。
附录,我认为这会有所帮助,这是Headers在回复中的样子:
General:
Request URL: http://rpc.local/api/payment_log_api.py
Request Method: OPTIONS
Status Code: 200 OK
Remote Address: 10.1.20.233:80
Referrer Policy: no-referrer-when-downgrade
Response Headers:
Allow: GET,HEAD,POST,OPTIONS
Connection: Keep-Alive
Content-Length: 0
Content-Type: text/x-python
Date: Fri, 27 Apr 2018 15:18:55 GMT
Keep-Alive: timeout=5, max=100
Server: Apache/2.4.18 (Ubuntu)
Request Headers:
Accept: */*
Accept-Encoding: gzip, deflate
Accept-Language: en-US,en;q=0.9
Access-Control-Request-Headers: authorization,content-type
Access-Control-Request-Method: POST
Connection: keep-alive
Host: rpc.local
Origin: http://10.1.20.61:4200
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
答案 0 :(得分:1)
在这里。只需在开头将其添加到应用程序:
def application(environ, start_response):
if environ['REQUEST_METHOD'] == 'OPTIONS':
start_response(
'200 OK',
[
('Content-Type', 'application/json'),
('Access-Control-Allow-Origin', '*'),
('Access-Control-Allow-Headers', 'Authorization, Content-Type'),
('Access-Control-Allow-Methods', 'POST'),
]
)
return ''
答案 1 :(得分:0)
对于具有CGI的Python,我发现它可以工作:
densityblocks = []
# make an empty list for each block
for i in range(num_blocks):
densityblocks.append([])
# Divide densities into blocks
for k in range(len(depth)):
if depth[k] < 500:
densityblocks[0].append(density[k])
elif depth[k]<1000:
densityblocks[1].append(density[k])
# Continue with elif for number of blocks
# calculate average for each block
avg_density = []
for i in range(len(densityblocks)):
for j in range(len(densityblocks[i])):
sum = sum + densityblocks[i][j]
avg_density.append(sum/len(densityblocks[i]))
别忘了在另一侧也启用CORS,例如JavaScript jQuery:
print '''Access-Control-Allow-Origin: *\r\n''',
print '''Content-Type: text/html\r\n'''