Embracing Android: Compiling Python to build a mobile wireless server platform

Source: Internet
Author: User
Tags locale

Do you want to use the old Android phone as a home server?

It's not that hard actually. Previously, using the Android SDK to develop a mobile app, install the next APK can host the service, and now directly native it.

This article will take you through the process of compiling Python and using Python to build a server that can run with you.


First, we're going to start compiling python on arm's Android platform. Of course, you need to have a Linux machine ready and then download and install the Android NDK from the Android website (preferably the SDK).

Download some of the necessary code packages:


openssl-1.0.1j:http://www.openssl.org/source/

ncurses-5.9:http://ftp.gnu.org/gnu/ncurses/

ReadLine 6.3:http://ftp.gnu.org/gnu/readline/

Sqlite-autoconf-3080701:http://www.sqlite.org/download.html

python-2.7.8:https://www.python.org/downloads/release/python-278/


We need to build these packages one by one:

1. common.sh: This file contains some basic settings, such as how to configure the selected Gcc,cflags and Ldflags.

Export ndkdir= "/Your NDK path such as/android-ndk-r10c" # GCC version selection, currently has 4.6,4.8,4.9, the choice is also note the type of Linux system, here x86_64 is 64-bit export COMPILER = "$NDKDIR/toolchains/arm-linux-androideabi-4.9/prebuilt/linux-x86_64/bin" Export cc= "$COMPILER/ ARM-LINUX-ANDROIDEABI-GCC "Export cxx=" $COMPILER/arm-linux-androideabi-g++ "Export cpp=" $COMPILER/ Arm-linux-androideabi-cpp "Export ld=" $COMPILER/arm-linux-androideabi-ld "Export as=" $COMPILER/ Arm-linux-androideabi-as "Export ar=" $COMPILER/arm-linux-androideabi-ar "Export strip=" $COMPILER/ Arm-linux-androideabi-strip "Export objcopy=" $COMPILER/arm-linux-androideabi-objcopy "Export objdump=" $COMPILER/ Arm-linux-androideabi-objdump "Export ranlib=" $COMPILER/arm-linux-androideabi-ranlib "Export nm=" $COMPILER/ ARM-LINUX-ANDROIDEABI-NM "Export strings=" $COMPILER/arm-linux-androideabi-strings "Export readelf=" $COMPILER/ Arm-linux-androideabi-readelf "# Choose which Android version you want to compile the file on, here is the case on Android 4.2, which is Android 17;2.2 corresponding to Android 8,5.0 corresponds to ANDROID 21export android= "$NDKDIR/platforms/android-17/arch-arm/usr" # Configure system header Fileand library file location export cflags= "-i$android/include--sysroot= $ANDROID" Export cxxflags= "-i$android/include--sysroot= $ANDROID" Export cppflags= "-i$android/include" Export ldflags= "-l$android/lib"

2. Compile OpenSSL:

Tar zxf openssl-1.0.1j.tar.gzcd openssl-1.0.1jmkdir distsource common.shmachine=armv7 SYSTEM=android./config-fpic-- prefix=./dist# in makefile to make some patches in case errorsed-i "s|-m64| |" Makefilesed-i "S|-wall|-wall--sysroot= $ANDROID |" makefile# Compiling and installing Makemake install

3. Compiling ncurses,readline,sqlite

The other three libraries are compiled in the same way as OpenSSL, but there is also a zlib that needs to be compiled and, of course, not let Python support bz2 be ignored.

Note ReadLine Compilation is best to choose--with-curses, and then put the compiled ncurses link on. Library class file compilation, as far as possible add-fpic, this is what, not from the assembly said really not clear, or everyone to see the official documents it.

For the localeconv problem, we'd better rewrite the locale.h, in the localeconv in the struct fix put on the characters you want, such as Decimal_point is ".", so there is no such locale problem.

The quick solution here is to kill the Localeconv and HardCode directly:

# before ncurses compile, you need to hit the patch sed-i "s/#define ISDECIMALPOINT (c). */#define ISDECIMALPOINT (c) ((c) = = '. ') /"Form/fty_num.csed-i" S/localeconv ()/null/"form/fty_num.c

4. Compiling python:

In fact, there is no difference between the whole process and OpenSSL, there are some precautions in detail.

-Configure files need to be manually fix, open files, search ac_cv_file__dev_ptmx and AC_CV_FILE__DEV_PTC, delete the automatic judgment of these two variables. Manually go to Android to see if there are PTMX and PTC devices in the/dev folder, yes No:

Ac_cv_file__dev_ptmx=yesac_cv_file__dev_ptc=no

-Open the Modules/setup.dist file, the required Python module in front of the # removed, such as #_socket socketmodule.c timemodule.c, to Python support network socket interface, you need to remove the # It is recommended that you try to add as many packages as possible; not really compiled packages, some modules need to download additional open source repositories compiled, not just openssl,ncurses,readline,sqlite.

-a compilation will be error, check carefully, found that Python needs to compile a program, this program runs on the host, but GCC is arm, host Linux is x86_64, so we need to copy a extracted Python code, and then the local original GCC compiled And, of course, compile directly./configure && make on it, until Pgen the executable file appears under the parser folder, take it out, copy it to another Python source parser folder, modify the makefile:

Sed-i "s|\$ (Pgen):. *|\$ (Pgen): |" Makefilesed-i "s|\$ (CC) \$ (OPT) \$ (ldflags) \$ (PGENOBJS) \$ (LIBS)-O \$ (pgen) |echo \" Fake parser/pgen\ "| Makefile

-Solve the locale problem, there are some constant problems, the stupid method HardCode:

Sed-i "s|. *localeconv (). *| | " Objects/stringlib/localeutil.hsed-i "s|locale_data->grouping|\" \ "|" Objects/stringlib/localeutil.hsed-i "s|locale_data->thousands_sep|\" \ "|" Objects/stringlib/localeutil.hsed-i "s|. *localeconv (). *| | " Objects/stringlib/formatter.hsed-i "s|locale_data->grouping|\" \ "|" Objects/stringlib/formatter.hsed-i "s|locale_data->thousands_sep|\" \ "|" Objects/stringlib/formatter.hsed-i "s|locale_data->decimal_point|\". \ "|" Objects/stringlib/formatter.hsed-i "s|. *localeconv (). *| | " Python/pystrtod.csed-i "s|locale_data->decimal_point|\". \ "|" Python/pystrtod.csed-i "s| I_push|0x5302| " Modules/posixmodule.csed-i "s|p->pw_gecos|\" \ "|" Modules/pwdmodule.c

-modules/socketmodule.c: Need to remove some # if, otherwise there is no definition in the header file, or go directly to $android's include folder to complete the corresponding. h file.

...    Py_begin_allow_threads#ifdef use_gethostbyname_lock    Pythread_acquire_lock (Netdb_lock, 1); #endif    h = GETHOSTBYADDR (AP, AL, af);    Py_end_allow_threads    ret = Gethost_common (h, (struct sockaddr *) &addr, sizeof (addr), AF); #ifdef use_ Gethostbyname_lock    Pythread_release_lock (netdb_lock); #endif    return ret;

Make and then make-i install, OK, Python compiles!


The following is a run on Android.

Need Android is root, not root can also, just need to find a place to put.

The Python-compiled folder needs to be packaged on Android and the so file in SQLite.

Root can be soft-linked to a python in/system/bin. Of course, sqlite.so.3 should be put in/system/lib.

In fact, SQLite can not compile, but our Django need it, so still get out of it, SSL can not be used, but for the server to support HTTPS, or compile it.

This will allow Python to run.

# python>>> 1+23
Then download Setuptools (https://pypi.python.org/pypi/setuptools/7.0) and Pip (https://pypi.python.org/pypi/pip/1.5.6) to unzip and install:

Tar zxf setuptools-7.0.tar.gzcd setuptools-7.0python setup.py buildpython setup.py installtar zxf PIP-1.5.6.TAR.GZCD Pip-1.5.6python setup.py buildpython setup.py Install
Send pip soft link to/system/bin. Well, Python has a pip, haha, install the package as you please. Let's start with a PIP install virtualenv.
Next you can install the Django Django-sslserver, the Django-admin soft link to/system/bin, you can write the website:

Django-admin startproject test001cd test001python manage.py migratepython manage.py runserver 0.0.0.0:8000
You can also provide HTTP services directly to a folder without Django:

Python-m Simplehttpserver

With the server, in the family can build a platform, if there are multiple mobile phones, connected to WiFi, you can do without wiring, complete wireless distributed server, quickly practice LoadBalance bar.

Well, look at the need to use the mobile phone server at any time to monitor the activities of the family, and then plug in the SIM card can also automatically send me text messages, hey.


Let's imagine how to solve the problem that pip install sometimes needs to compile C files. Actually, a team has solved this problem.

Download droid for GCC plugin apk:http://www.liqucn.com/rj/228351.shtml (This is not the official website, preferably to Google Play download)

Unzip the APK, and then find the GCC tarball, with GCC in it and put it on Android:

#include <stdio.h>int main () {  printf ("Hello world!\n");  return 0;}
Then Gcc-o test test.c, and run the./test, the perfect output Hello world. Quickly soft link to/system/bin.
OK, so numpy can be compiled and installed. can also compile the Erl, the RABBITMQ compiled, get a distributed is not a problem. It's best to transplant lxc and then move the arm version of Java in raspberry to be invincible.

Overall, you can set up a mobile server, later to write some web version of small applications, when you want to use Android to open a hot spot, computer, start enjoy!

Let the server everywhere, start from home ~


2014.11.26

J.y.liu



Embracing Android: Compiling Python to build a mobile wireless server platform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.