我的程序是从web获取参数(URL),在我的龙卷风API中,我想将它们接受为一个再见。现在我的代码接受一个元组。我的api想要获取来自web URL请求的4个参数(south-lat,south-long,east-lat,east-long)。我将它们实现为Tornado的get方法并在我的数据库中找到它们。我想知道如何在龙卷风中单独接受它们。这是我目前的工作代码。
import socket
import tornado.web
from datetime import datetime
import pymongo
from pymongo import MongoClient
from pprint import pprint
import tornado.httpserver
import tornado.ioloop
import tornado.options
from tornado.options import define,options
#apicall/v1/geoloc.json?geoquery=True&southwest_lat=''&southwest_lng=''&northeast_lat=''&northeast_lng=''
#tornado port
port = 8088
host = "127.0.0.1"
#make a connection with mongodb
#client=MongoClient()
class MainHandler(tornado.web.RequestHandler):
def get(self):
self.write("hello APIs")
#what this works is here
class VisualizationHandler(tornado.web.RequestHandler):
def get(self,*args):#how to accept the input from map?
self.open_args=args
#self.open_kwargs=kwargs
print(self.open_args)
print(type(self.open_args))
#self.write("Data is at the terminal")
#client = MongoClient("mongodb://localhost:27017")
#db=client.test
#docs=db.neighborhoods.findOne()
#if docs is None:
#print("Not found")
#else:
#print(docs)
#lat=self.get_argument('lat',True)
#long=self.get_argument('long',True)
#self.write(lat)
#self.write(long)
#var neighborhood = db.neighborhoods.findOne( { geometry: { $geoIntersects: { $geometry: { type: "Point", coordinates: [ slong, slat] } } } } )
def main():
application = tornado.web.Application([
(r"/", MainHandler),
(r"/geo/(.*)",VisualizationHandler),
])
try:
sockets = tornado.netutil.bind_sockets(port, address=host)
print("Socket binding successful to {0}:{1}".format(host, port))
except socket.error:
print("Socket binding failed to {0}:{1}".format(host, port))
return
try:
_id = tornado.process.fork_processes(0, max_restarts=3)
print("Process forked : {}".format(_id))
server = tornado.httpserver.HTTPServer(application)
server.add_sockets(sockets)
tornado.ioloop.IOLoop.current().start()
except KeyboardInterrupt: # stop with Ctrl+C from shell
print("Tornado is stopping")
if __name__ == "__main__":
print("Tornado is starting")
main()
答案 0 :(得分:0)
get()方法的args / kwargs是从url中检索的(url正则表达式的匹配组,具体而言)。当然,你可以创建这样的东西来将参数传递给你的函数:
(r"/geo/(.*)/(.*)", VisualizationHandler)
或
(r"/geo/(?P<south_lat>.*)/(?P<south_long>.*)", VisualizationHandler)
和args / kwargs south_lat和south_long将从/ geo / 123/456这样的URL传递给你的函数,但这很不方便。我建议你将你的论点作为URL params / geo?south_lat = 123&amp; south_long = 456传递,并使用get_argument()读取它们。