Django - 使用 XML - RESTful

发布于 2024-10-27 02:58:59 字数 953 浏览 3 评论 0原文

我有一个 python 脚本在我的本地主机上运行良好。它不是一个企业应用程序或任何东西,只是我正在玩的东西。它使用“bottle”库。该应用程序基本上使用一个 XML 文件(本地存储或在线存储),其中包含具有自己唯一 ID 的元素以及一些坐标,例如 mysite.com/23 将带回元素 23 的纬度/经度。我确信无论如何,在这个阶段你们都已经熟悉了 REST。

现在,我想把它放到网上,但找不到支持“bottle”的主机。然而,我找到了一个安装了 django 的主机。

所以,我的问题是,将以下代码从 Bottle 转换为 django 有多难?有人可以给我一些指点吗?我尝试过使用常见的 python 库。

谢谢。

from xml.dom.minidom import parseString
from bottle import route, run
import xml
import urllib

file = open('myfile.xml','r')
data = file.read()
dom = parseString(data)
@route('/:number')
def index(number="1"):
    rows = dom.getElementsByTagName("card")[0].getElementsByTagName("markers")[0].getElementsByTagName("marker")
    for row in rows:
        if row.getAttribute("number") == str(number):
             return str(xml.dumps({'long': row.getAttribute("lng"), 'lat': row.getAttribute("lat")}, sort_keys=True, indent=4))
    return "Not Found"


run(host='localhost', port=8080)

I have a python script running fine on my localhost. Its not an enterprise app or anything, just something I'm playing around with. It uses the "bottle" library. The app basically consumes an XML file (stored either locally or online) which contains elements with their own unique IDs, as well as some coordinates, eg mysite.com/23 will bring back the lat/long of element 23. I'm sure you're all familiar with REST at this stage anyway.

Now, I want to put this online, but have had trouble finding a host that supports "bottle". I have, however, found a host that has django installed.

So, my question is, how hard would it be to convert the following code from bottle to django? And can someone give me some pointers? I've tried to use common python libraries.

thanks.

from xml.dom.minidom import parseString
from bottle import route, run
import xml
import urllib

file = open('myfile.xml','r')
data = file.read()
dom = parseString(data)
@route('/:number')
def index(number="1"):
    rows = dom.getElementsByTagName("card")[0].getElementsByTagName("markers")[0].getElementsByTagName("marker")
    for row in rows:
        if row.getAttribute("number") == str(number):
             return str(xml.dumps({'long': row.getAttribute("lng"), 'lat': row.getAttribute("lat")}, sort_keys=True, indent=4))
    return "Not Found"


run(host='localhost', port=8080)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我们的影子 2024-11-03 02:58:59

我把你的问题作为进一步了解 Django 的机会。我使用 The Django Book 作为参考。

从一个空的 Django 站点 (django-admin.py startproject testsite) 开始,我将 urls.py 更改为:

from django.conf.urls.defaults import patterns, include, url
from testsite.views import index

urlpatterns = patterns('',
    url(r'^(\d+)

views.py > 对此:

from django.http import HttpResponse
from xml.dom.minidom import parseString
import xml
import urllib

def index(request, number):
    data = open('myfile.xml', 'r').read()
    dom = parseString(data)
    rows = (dom.getElementsByTagName("card")[0]
               .getElementsByTagName("markers")[0]
               .getElementsByTagName("marker"))

    for row in rows:
        if row.getAttribute("number") == str(number):
             return HttpResponse(str(xml.dumps({'long': row.getAttribute("lng"), 
                    'lat': row.getAttribute("lat")}, sort_keys=True, indent=4)))
    return HttpResponse("Not Found")

警告:我没有测试 XML 代码,只测试了与 Django 相关的代码,我已经通过 python manage.py runserver 对其进行了测试。

Django Book 包含大量信息,包括如何在生产服务器上部署它。

, index), )

views.py > 对此:

警告:我没有测试 XML 代码,只测试了与 Django 相关的代码,我已经通过 python manage.py runserver 对其进行了测试。

Django Book 包含大量信息,包括如何在生产服务器上部署它。

I took your question as an opportunity to learn a bit more about Django. I used The Django Book as a reference.

Starting with an empty Django site (django-admin.py startproject testsite), I've changed urls.py to this:

from django.conf.urls.defaults import patterns, include, url
from testsite.views import index

urlpatterns = patterns('',
    url(r'^(\d+)

And views.py to this:

from django.http import HttpResponse
from xml.dom.minidom import parseString
import xml
import urllib

def index(request, number):
    data = open('myfile.xml', 'r').read()
    dom = parseString(data)
    rows = (dom.getElementsByTagName("card")[0]
               .getElementsByTagName("markers")[0]
               .getElementsByTagName("marker"))

    for row in rows:
        if row.getAttribute("number") == str(number):
             return HttpResponse(str(xml.dumps({'long': row.getAttribute("lng"), 
                    'lat': row.getAttribute("lat")}, sort_keys=True, indent=4)))
    return HttpResponse("Not Found")

Caveat: I've not tested the XML code, only Django-related one, which I've tested via python manage.py runserver.

The Django Book contains a lot of information, including how to deploy this on a production server.

, index), )

And views.py to this:

Caveat: I've not tested the XML code, only Django-related one, which I've tested via python manage.py runserver.

The Django Book contains a lot of information, including how to deploy this on a production server.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文