【发布时间】:2015-04-16 09:32:44
【问题描述】:
我有一个 Flask 应用程序,它侦听传入的 Shopify webhook,执行某些操作,然后响应 200。但是,Shopify 继续发送 webhook,就好像我的应用程序响应了 200 以外的东西一样。
当我用 curl 模拟这个时:
curl -H "Content-Type: application/json" -D /tmp/dump.log -X POST -d @<valid json> <my server>
响应头是:
HTTP/1.1 100 Continue
HTTP/1.1 200 OK
Date: Thu, 16 Apr 2015 09:25:23 GMT
Server: Apache/2.4.7 (Ubuntu)
Content-Length: 0
Content-Type: text/html; charset=utf-8
我相信第一个 100 Continue 是让 Shopify 认为我的应用失败的原因。我在本地实例和生产环境中都得到了相同的响应。
发生了什么,我该如何解决?
编辑
这是处理请求的python代码
import json
from flask_mail import Mail, Message
from pyPdf import PdfFileWriter, PdfFileReader
from flask import Flask
from certificate import Certificate
from flask import request
from purchase import Purchase
from stars import Stars
app = Flask(__name__)
app.config.from_envvar('NAMESTAR_SETTINGS')
app.config["MAIL_SERVER"] = 'smtp.sendgrid.net'
app.config["MAIL_PORT"] = 587
app.config["MAIL_USERNAME"] = app.config.get('EMAIL_LOGIN')
app.config["MAIL_PASSWORD"] = app.config.get('EMAIL_PASSWORD')
app.config["MAIL_DEFAULT_SENDER"] = app.config.get('SENDER')
app.config["PATH"] = app.config.get('PATH')
ADMINS = app.config.get('ADMINS')
mail = Mail(app)
def get_request():
if request.args.get('hyg') is not None:
return int(request.args.get('hyg'))
else:
return request.data
#returns an instance of Purchase class based on the request
def get_purchase():
pass
#creates and saves a PDF created with reportlab
def generate_certificate(purchase):
pass
#sends the generated PDF to the recipient
def send_email(certificate_path, recipient, name=""):
msg = Message("Message", recipients=[recipient])
with app.open_resource(certificate_path) as fp:
msg.attach("certificate.pdf", "application/pdf", fp.read())
f = open(app.config.get('PATH') + "/email_templates/certificate", "r")
html = f.read()
msg.html = html % (name)
mail.send(msg)
@app.route('/', methods=['GET', 'POST'])
def generate():
try:
purchase = get_purchase()
certificate_path = generate_certificate(purchase)
send_email(certificate_path, purchase.customer_email(), name=purchase.customer_name())
except Exception as e:
raise
return ('', 200)
if __name__ == '__main__':
app.debug = False
app.run(debug=False)
服务器是 Apache,这是应用程序的虚拟主机设置
<VirtualHost *:80>
DocumentRoot /var/www/namestar/current
WSGIDaemonProcess namestar user=www-data group=www-data threads=5
WSGIScriptAlias / /var/www/namestar/current/namestar.wsgi
<Directory /var/www/namestar/current>
WSGIScriptReloading On
WSGIProcessGroup namestar
WSGIApplicationGroup %{GLOBAL}
Order deny,allow
Allow from all
</Directory>
SetEnv NAMESTAR_SETTINGS="/var/www/namestar/current/config/production.cfg"
</VirtualHost>
【问题讨论】:
标签: python http flask shopify webhooks