我需要将JSON从客户端POST到服务器.我正在使用Python 2.7.1和simplejson.客户端正在使用请求.服务器是CherryPy.我可以从服务器获取硬编码的JSON(代码未显示),但是当我尝试将JSON发送到服务器时,我得到"400 Bad Request".
这是我的客户端代码:
data = {'sender': 'Alice',
'receiver': 'Bob',
'message': 'We did it!'}
data_json = simplejson.dumps(data)
payload = {'json_payload': data_json}
r = requests.post("http://localhost:8080", data=payload)
Run Code Online (Sandbox Code Playgroud)
这是服务器代码.
class Root(object):
def __init__(self, content):
self.content = content
print self.content # this works
exposed = True
def GET(self):
cherrypy.response.headers['Content-Type'] = 'application/json'
return simplejson.dumps(self.content)
def POST(self):
self.content = simplejson.loads(cherrypy.request.body.read())
Run Code Online (Sandbox Code Playgroud)
有任何想法吗?
我的本地局域网(machineA)上有一台有两台Web服务器的机器.第一个是XBMC中的内置版(在端口8080上)并显示我们的库.第二个服务器是CherryPy python脚本(端口8081),我用它来按需触发文件转换.文件转换由来自XBMC服务器提供的页面的AJAX POST请求触发.
jQuery Ajax请求
$.post('http://machineA:8081', {file_url: 'asfd'}, function(d){console.log(d)})
Run Code Online (Sandbox Code Playgroud)
请求标题 - 选项
Host: machineA:8081
User-Agent: ... Firefox/4.01
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 115
Connection: keep-alive
Origin: http://machineA:8080
Access-Control-Request-Method: POST
Access-Control-Request-Headers: x-requested-with
Run Code Online (Sandbox Code Playgroud)
响应标题 - 选项(状态= 200 OK)
Content-Length: 0
Access-Control-Allow-Headers: *
Access-Control-Max-Age: 1728000
Server: CherryPy/3.2.0
Date: Thu, 21 Apr 2011 22:40:29 GMT
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: POST, GET, OPTIONS
Content-Type: text/html;charset=ISO-8859-1
Run Code Online (Sandbox Code Playgroud)
为了排除故障,我还从http://jquery.com发出了相同的$ .post命令.这是我难倒的地方,从jquery.com开始,post请求有效,OPTIONS请求由POST发送.此交易的标题如下;
请求标题 - …
我正在0.0.0.0:8787EC2实例上运行CherryPy Web服务器.
我可以通过wgetEC2机器上的本地连接到Web服务器,但是我无法从我自己的远程机器上访问该实例(我通过ssh连接到EC2).
我是否需要打开端口8787以远程访问Web服务器?如果是这样,怎么办呢?另外,我可以使用EC2实例的公共IP吗?
在此先感谢您的帮助!
# ! /usr/bin/env python
# -*- coding: utf-8 -*-
# login_frontend.py
""" Python 2.7.3
Cherrypy 3.2.2
PostgreSQL 9.1
psycopy2 2.4.5
SQLAlchemy 0.7.10
"""
Run Code Online (Sandbox Code Playgroud)
我在一个Python/SQLAlchemy类中连接四个表时遇到问题.我正在尝试这个,所以我可以迭代这个类的实例,而不是我从连接ORM表中得到的命名元组.
为什么这一切?因为我已经开始这样了,而且我走得太远,只是离开它.此外,它必须是可能的,所以我想知道它是如何完成的.
对于这个项目(cherrypy web-frontend),我得到了一个已经完成的包含表类的模块.我把它移到了这篇文章的底部,因为也许你甚至都不需要它.
以下只是联接多表类尝试的一个示例.我选择了一个简单的案例,其中只有两个表和一个联结表.在这里,我不会写入这些连接表,但它在其他地方是必要的.这就是为什么类可以很好地解决这个问题.
这是给定的表类模块和这两个网站的示例的组合:
- 针对多个表映射类
- SQLAlchemy:一个类 - 两个表
class JoinUserGroupPerson (Base):
persons = md.tables['persons']
users = md.tables['users']
user_groups = md.tables['user_groups']
groups = md.tables['groups']
user_group_person =(
join(persons, users, persons.c.id == users.c.id).
join(user_groups, users.c.id == user_groups.c.user_id).
join(groups, groups.c.id == user_groups.c.group_id))
__table__ = user_group_person
""" I expanded the redefinition of 'id' to three tables,
and …Run Code Online (Sandbox Code Playgroud) SQLAlchemy是否支持某种缓存,所以如果我重复运行相同的查询,它会从缓存中返回响应而不是查询数据库?更新数据库时是否自动清除此缓存?
或者在CherryPy + SQLAlchemy设置上实现此功能的最佳方法是什么?
我遵循了基本的CherryPy教程(http://www.cherrypy.org/wiki/CherryPyTutorial).没有讨论的一件事是部署.
如何启动CherryPy应用程序作为守护进程并"忘记它"?如果服务器重新启动会发生什么?
有标准食谱吗?也许会创建一个服务脚本(/etc/init.d/cherrypy ...)
谢谢!
如何从CherryPy中的POST请求接收JSON?
我去过这个页面,虽然它很好地解释了API,它的参数以及它的作用; 我似乎无法弄清楚如何使用它们将传入的JSON解析为一个对象.
这是我到目前为止所拥有的:
import cherrypy
import json
from web.models.card import card
from web.models.session import getSession
from web.controllers.error import formatEx, handle_error
class CardRequestHandler(object):
@cherrypy.expose
def update(self, **jsonText):
db = getSession()
result = {"operation" : "update", "result" : "success" }
try:
u = json.loads(jsonText)
c = db.query(card).filter(card.id == u.id)
c.name = u.name
c.content = u.content
rzSession.commit()
except:
result["result"] = { "exception" : formatEx() }
return json.dumps(result)
Run Code Online (Sandbox Code Playgroud)
而且,这是我的jquery电话来发帖
function Update(el){
el = jq(el); // makes sure that this is a …Run Code Online (Sandbox Code Playgroud) 在Cherrypy中,可以这样做:
@cherrypy.expose
def default(self, url, *suburl, **kwarg):
pass
Run Code Online (Sandbox Code Playgroud)
是否有相同的烧瓶?