简体   繁体   English

如何从Mac应用程序向Django服务器发送HTTP请求?

[英]How to send an HTTP request from a Mac app to a Django server?

I want to send an HTTP request like: 我想发送一个HTTP请求,如:

"http://.../mydjangosite?var1=value1&var2=value2&var3=value3"

to my Django server and want this last to send response with a boolean value "1" if value1,2,3 are equal to the ones in my database and "0" if not. 到我的Django服务器,并希望最后发送一个布尔值"1"如果value1,2,3等于我的数据库中的值)和"0"如果不相等)的响应。

This is the first time I develop with Django so if anyone has tips to give me I would be grateful. 这是我第一次使用Django进行开发,因此如果有人给我提示,我将不胜感激。

Sorry for bad English, if anyone didn't understand my question please feel free to tell it to me. 对不起,英语不好,如果有人听不懂我的问题,请随时告诉我。

Kind regards. 亲切的问候。

EDIT : 编辑:

first of all thanks for your fast answers!! 首先感谢您的快速回答!

You're right i'm too generic i'll explain more precisly. 您是对的,我太笼统了,我会更准确地解释。 I'm working on a project where we have some mac applications. 我正在做一个有一些Mac应用程序的项目。 We want to create a "plateform" where clients of those applications would be able to get last versions developped of those lasts. 我们希望创建一个“平台”,这些应用程序的客户端将能够从这些平台开发最新版本。 The purpose is then to create at first, a django server where we store informations about version of the software..etc. 然后的目的是首先创建一个django服务器,我们将在其中存储有关该软件的版本信息。 And then, when user of the software execute it, it'll send automaticaly an http request to the server in order to "check" if a new version exists. 然后,当软件的用户执行该软件时,它将自动向服务器发送一个http请求,以便“检查”是否存在新版本。 If yes, we invite him to download new version, if no, then it continues. 如果是,我们邀请他下载新版本,如果不是,则继续。

From now, i've been working on the django server, i started with the tutorial at django's site. 从现在开始,我一直在django服务器上工作,我从django网站上的教程开始。 I created my models.py example : 我创建了models.py示例:

class Version(models.Model):
    name = models.CharField(max_length = 200)
    description = models.TextField()
    id_version = models.IntegerField()
    date_version = models.DateTimeField('Version published')
    def was_published_today(self):
            return self.date_version.date() == datetime.date.today()
    def get_number_version(self):
            return "%d" % (self.id_version)
    def save(self):
            top = Version.objects.order_by('-id_version')[0]
            self.id_version = top.id_version + 1
            super(Version, self).save()

I changed the urls.py like that : 我这样改变了urls.py:

urlpatterns = patterns('',
# Examples:

# Uncomment the next line to enable the admin:
url(r'^admin/', include(admin.site.urls)),
url(r'^versionsupdate/version/$','versionsupdate.views.version'),

So, what I want, is from a mac app, send an http request to the server django like that "http://.../versionsupdate/version?version=1..." and then, in my server catch the request, get the value after the "=" compare it to the value of "id_version" and response back with a boolean value depending on if it equals or not. 因此,我要从Mac应用程序中向服务器django发送HTTP请求,例如“ http://.../versionsupdate/version?version = 1 ...”,然后在服务器中捕获请求,将“ =”之后的值与“ id_version”的值进行比较,然后返回一个布尔值(取决于它是否相等)。

I hope this is clear i'm not sure^^ and please tell me if what i did from now is good or not, i'm new to django/python so i'm note sure to be on the good direction. 我希望这很清楚,我不确定^^,请告诉我我从现在开始所做的事情是否好,我是django / python的新手,所以我要确保方向是正确的。

Thanks again for your help! 再次感谢你的帮助!

First. 第一。 From Django-side you need to specific data type response (render_to_response headers). 从Django方面,您需要特定的数据类型响应(render_to_response标头)。 For example it can be json. 例如,它可以是json。

Second. 第二。 From python script on client-side you can get url using urllib or urllib2. 从客户端的python脚本中,您可以使用urllib或urllib2获取url。

import urllib
import urllib2

url = 'http://www.acme.com/users/details'
params = urllib.urlencode({
  'firstName': 'John',
  'lastName': 'Doe'
})
response = urllib2.urlopen(url, params).read()

Then just analyze response. 然后,只需分析响应即可。

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

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