简体   繁体   中英

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

I want to send an HTTP request like:

"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.

This is the first time I develop with Django so if anyone has tips to give me I would be grateful.

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. 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. 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. 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. I created my models.py example :

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 :

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.

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.

Thanks again for your help!

First. From Django-side you need to specific data type response (render_to_response headers). For example it can be json.

Second. From python script on client-side you can get url using urllib or urllib2.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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