[英]Python POST sql query result
I want to send a bunch of results to a server. 我想将一堆结果发送到服务器。
cur.execute('SELECT a,b,c,b FROM wicap WHERE a.num > 600 limit 25')
r = cur.fetchall()
r at this time is: 此时的r是:
[(1487590224, 1487614532, -75, -41, 504, -73),
(1487596562, 1487614915, -75, -59, 156, -75),...]
I want to do something like: 我想做类似的事情:
response = urllib2.urlopen('http://example.com/post/', data=r)
And on the server side be able to get the data via: 在服务器端,可以通过以下方式获取数据:
def POST()
data = web.data()
for record in data...
and then process the query result in the server. 然后在服务器中处理查询结果。
urlencode fails with ValueError: too many values to unpack I've tried creating a json with res = [dict((cur.description[i][0], value) for i, value in enumerate(row)) for row in r] but f = urllib2.urlopen(_url,res) returns TypeError: must be string or buffer, not list urlencode失败并出现ValueError:太多值无法解包我尝试用res = [dict((cur.description [i] [0],value)for i,enumerate(row))中的值创建一个json ],但f = urllib2.urlopen(_url,res)返回TypeError:必须为字符串或缓冲区,而不是列表
Ideas are welcome! 欢迎提出想法!
You need to encode the data to json with json.dumps
: 您需要使用json.dumps
将数据编码为json:
import json
r = [(1487590224, 1487614532, -75, -41, 504, -73), (1487596562, 1487614915, -75, -59, 156, -75)]
response = urllib2.urlopen('http://example.com/post/', data=json.dumps(r))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.