Tag Archives: Python

Sending iOS Push Notifications using Python module PyAPNS

python-django

I have written a small module, that sends a push notification using PyAPNS.

This is how my sender.py module looks like:

Here is a Sender class inside sender.py module. It has one method send. Inside this method I create an object of APNs class, create a payload and send it using this object.
Cert.pem and Key.pem files are inside the folder with this module. They can be created using this tutorial. Also that folder contains apns.py module file with PyAPNS code.

To send a sample push notification from a command line I use following commands. First, I enter the folder with module. It is called notification_sender:

Or on server:

Then I enter to the python console:

Then I import Sender class from sender module:

Create a sender object of a Sender class:

And finally send a notification:

All you need to do to finish the job is to enter a PEM pass phrase:

Currently, the device token is hardcoded, I am planning to use SQLite entries with collected tokens. I use Django to collect tokens.

I would like to add that if you change somehow the source file of a module, you should reenter python console and recreate object to make changes work.

Running Django project on Apache production server with mod_wsgi

django

This is official tutorial. But it didn’t work for me for 100 %. But there is some useful information on wsgi and daemon process that should be created.

There is a good tutorial, that I followed and it actually works. But I decided to write my own post with instructions for my personal project:

First we enter the production server via SSL and create a virtual environment there:

Then we activate it:

Check the Django version. It must be the same as in your local server:

Save somewhere a path to a Django project:

Your path will be different.

This is an example of a sites-available/*.conf file setting to run Django with WSGI:
Снимок экрана 2014-09-19 в 20.25.49

Here app_server is the name of my Django project or you can name it a site. But in my case it is not site but server for a mobile application.

Python and Django version on server and virtual environment may be different. You should do everything inside virtual environments with the same versions on local and production servers.

Installing Django on Mac

django

First you should install Python and Pip:

Then you install virtual environments:

And activate one virtual environment:

Then you install Django using pip in that virtual environment. Don’t use sudo, since if you use it, then you will install Django not in virtual environment but in common environment.

Now you can start creating Django projects using this tutorial.

To look at a version of installed Django use this:

To create a project:

To activate database:

To run a server:

To open your first project on Django:

Searching for the best technology for a mobile app backend development

Client-server applications with a mobile app client are more interesting than mobile standalone applications. They offer a new type of features for users like daily updated content. That is why at some moment of my mobile developer career I decided to learn how to create a server side for my mobile applications myself. As it happens usually in IT, there were a lot of techologies and programming languages, that I could start to learn and use for achieve my goals. Of course, if I would stop on the first one, that I have tried, I would lose, because one can never find the best way without understanding what other ways give and without any broad enough knowledge. I think, it is a good idea to get familiar with minuses of each technology before starting to learn and use it.

This is my story. I started with Java Servlets and I have tried also PHP and Python at the moment. Also I have read about Ruby On Rails and Node.JS. Finally, I stopped currently at using Python with Django.

Between Java Servlets and Python I choose Python. Development on Python it is much faster than on Java Servlets. It has no complex and sensible configuration files. Python offers iterative development, you can just refresh a web page and see results instantly. With Java Servlets you have to wait some time every time you update something. Python is like scripting language, despite it is compiled to some kind of byte code. I also liked python for it’s clean syntax, despite I am friendly with Java too. Using HTML templates with Python is easier to me, than creating JSP pages with servlets. Just for information, I use Sublime Text 2 as a Python editor with a Lazy color theme. It is really convenient editor.

I will not try PHP, because it is not valuable now, loses it’s popularity. It looks like Python offers almost the same features but even is better because of syntax, because it is compiled and because it offers full OOP support from the start and you can create all kind of applications including desktop applications with it. Also there are a lot of scientific and data analysis libraries on Python, that can be useful to create sophisticated back-end.

I don’t like the idea of using generally considered to be slow Javascript at the backend in Node.js. Also Node.js needs it’s own HTTP server.

What about Parse, QuickBlox and other ready for use backend services? I don’t want to use them, because I want to be able to create my own-backends with complete understanding of their code and work. These are good to create something easy and usual, but not something unique.

Probably I will try also Ruby on Rails at some moment.

But currently I am completely satisfied with Python. I want to get familiar with Django framework on Python too.

There are also open-source backends like Baasbox: http://www.baasbox.com/. But I haven’t tried it yet. Looks like it is written on Java, because needs JVM.

More articles on this topic:

http://www.raywenderlich.com/20482/how-to-choose-the-best-backend-provider-for-your-ios-app-parse-vs-stackmob-vs-appcelerator-cloud-and-more

http://www.developer-tech.com/news/2013/feb/22/exploring-mobile-app-backend-options/