Home > other >  Using the Python tornado module, to build a web service.
Using the Python tornado module, to build a web service.

Time:09-16

Python small white, would like to ask everyone here elder - using the tornado Python module, building a web services start page does not display the problem, as follows (code section for reference "to read" work on zhihu an answer address is: https://zhuanlan.zhihu.com/p/30742302 tort link delete)


# - * - coding: utf-8 - * -
The import sys
The import importlib
Importlib. Reload (sys)
The from reload the import *

# tornado use under Windows system using SelectorEventLoop
The import of platform

If platform. The system ()=="Windows" :
The import asyncio
Asyncio. Set_event_loop_policy (asyncio WindowsSelectorEventLoopPolicy ())
The import pandas as pd
The import tornado. Ioloop
The import tornado. Web
# import json
The import requests

Lists={u "shenzhen", "is a special economic zone, adjacent to guangzhou, which borders Hong Kong, a population of about 12 million",
U "Qingdao", "tourist city, near the yellow sea, specialty of beer, a population of about 9.2 million"
}

Def get_loc (city) :
R=requests. Get (" http://api.map.baidu.com/geocoder? Address=shenzhen & amp; The output=json& Key=37492 c0ee6f924cb5e934fa08c6b1676 & amp; City=8 c % E5 % % 97% B8 E5 AC BA E4 % % % % % 82 "% city. The encode (' utf-8))
Loc=r.j son ()
Df=pd DataFrame ((loc))
Str1=df. Loc [0, 'the result]
Str2=pd. DataFrame ([str1])
Return the print (" shenzhen latitude and longitude information for: ")
Return the print (str2. Loc [0, 'location'])
The class BaseHandler (tornado. Web. RequestHandler) :
Def get_current_user (self) :
Return the self. Get_secure_cookie (" user ")

The class MainHandler (BaseHandler) :
Def get (self) :
Global the greeting
If the "id" in the self. The request. The arguments:
The greeting=self. Get_argument (' id ', 'Hello')
If the greeting in lists:
Self. Write (the greeting + ":" + STR (get_loc (greeting)) + ", "+ STR (lists [the greeting]))
The else:
The self. The write (" none ")

Settings=dict (cookie_secret="P1/V61oETzdkLmGeJJFuYh7Eo5KXQAGaYgEQnp2XdTo=", the debug=True)
Application=tornado. Web application ([(r "/", MainHandler)], * * Settings)

If __name__=="__main__" :
Application. Listen (8086)
Tornado. Ioloop. Ioloop. Current (). The start ()

Description: 1, intermediate operating error, solved some after turn to baidu, the main problem is the result of the author is based on Python2.0 while I am in Python3. X done so some grammar have change,
2, the author gives a query city longitude and latitude API interface is not available, I through interface together with other processing can output the latitude and longitude information. But in writing web_server py don't know whether to call errors,
  • Related