简体   繁体   English

使用Google App Engine的Web服务

[英]Web Services with Google App Engine

I see that Google App Engine can host web applications that will return html, etc. But what about web services that communicate over http and accept / return xml? 我看到Google App Engine可以托管将返回html等的Web应用程序。但是通过http和accept / return xml进行通信的Web服务呢?

Does anyone know how this is being done in Goggle App Engine with Python or for that matter in Java (JAS-WX is not supported)? 有谁知道如何使用Python在Goggle App Engine中完成这项工作,或者在Java中是如何做到的(不支持JAS-WX)? Any links o samples or articles is greatly appreciated. 任何链接o样品或文章非常感谢。

Thanks // :) 谢谢 // :)

Google App Engine allows you to write web services that return any type of HTTP response content. Google App Engine允许您编写返回任何类型的HTTP响应内容的Web服务。 This includes xml, json, text, etc. 这包括xml,json,文本等。

For instance, take a look at the guestbook sample project offered by Google which shows the HTTP response coming back as text/plain: 例如,看一下Google提供的留言板示例项目 ,该项目显示以text / plain形式返回的HTTP响应:

    public class GuestbookServlet extends HttpServlet {
        public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
            UserService userService = UserServiceFactory.getUserService();
            User user = userService.getCurrentUser();

            if (user != null) {
                resp.setContentType("text/plain");
                resp.getWriter().println("Hello, " + user.getNickname());
            } else {
                resp.sendRedirect(userService.createLoginURL(req.getRequestURI()));
            }
        }
   }

Additionally, the app engine google group is a great place to learn more, ask questions, and see sample code. 此外, 应用引擎谷歌小组是一个了解更多,提问和看示例代码的好地方。

Most python apps just write a handler that outputs the shaped xml directly... this example serves any GET requests submitted to the root url ("/"): 大多数python应用程序只是编写一个直接输出整形xml的处理程序...此示例提供提交到根URL(“/”)的任何GET请求:

import wsgiref.handlers

from google.appengine.ext import webapp

class MainHandler(webapp.RequestHandler):

  def get(self):
    self.response.out.write('<myXml><node id=1 /></myXml>')

def main():
  application = webapp.WSGIApplication([('/', MainHandler)],
                                       debug=True)
  wsgiref.handlers.CGIHandler().run(application)

if __name__ == '__main__':
  main()

It's definitely possible (and not too hard) to use GAE to host "web services that communicate over http and accept / return xml". 使用GAE来托管“通过http进行通信并接受/返回xml的Web服务”绝对是可能的(而且不是太难)。

To parse XML requests (presumably coming in as the body of HTTP POST or PUT requests), you have several options, eg pyexpat or minidom on top of it, see this thread for example (especially the last post on it). 要解析XML请求(可能是作为HTTP POST或PUT请求的主体),您有几个选项,例如pyexpat或minidom,请参阅此主题 (例如,最后一篇文章)。

If you wish, you could also use minidom to construct the XML response and write it back (eg using a StringIO instance to hold the formatted response and its write method as the argument to your minidom instance's writexml method, then turning around and using the getvalue of that instance to get the needed result as a string). 如果您愿意,您还可以使用minidom构造XML响应并将其回(例如,使用StringIO实例保存格式化响应及其write方法作为minidom实例的writexml方法的参数,然后转向并使用getvalue该实例将所需结果作为字符串获取)。 Even though you're limited to pure Python and a few "whiteslisted" C-coded extensions such as pyexpat, that doesn't really limit your choices all that much, nor make your life substantially harder. 尽管你只限于纯Python和一些“白名单”的C编码扩展,例如pyexpat,但这并没有真正限制你的选择,也不会让你的生活变得更加艰难。

Just do remember to set your response's content type header to text/xml (or some media type that's even more specific and appropriate, if any, of course!) -- and, I recommend, use UTF-8 (the standard text encoding that lets you express all of Unicode while being plain ASCII if your data do happen to be plain ASCII!-), not weird "code pages" or regionally limited codes such as the ISO-8859 family. 只需要记住将响应的内容类型标题设置为text/xml (或者某种媒体类型,当然更具体和适当,如果有的话!) - 我建议使用UTF-8(标准文本编码,如果你的数据恰好是普通的ASCII,那么你可以用纯ASCII表示所有的Unicode! - ),而不是奇怪的“代码页”或区域有限的代码,如ISO-8859系列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM