Python Notes — Installing Python 3 on CentOS 6, a Web Server Checker
2016 · 02 · 172 min readPaper
Contents
Two posts from 2016, written while picking up Python for the first time, merged here.
They follow each other naturally: install it, then immediately build something with it.
Installing Python 3 on CentOS 6
CentOS 6 ships with Python 2.6. This builds 3.5.1 from source.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
yum install zlib-devel -y
yum install openssl openssl-devel -y
wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tar.xz
xz -d Python-3.5.1.tar.xz
# if xz is missing, install it with yum install xztar -xvf Python-3.5.1.tar
cd Python-3.5.1
./configure --prefix=/usr/local --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"make && make altinstall
# install pipcurl -k -O https://bootstrap.pypa.io/get-pip.py
python3.5 get-pip.py
make altinstall matters. make install overwrites the existing python and breaks
system tools like yum. altinstall only installs it under the name python3.5.
zlib-devel and openssl-devel go in first because without them the build still
succeeds, but pip cannot use SSL and installing packages fails.
Python is nice for small programs. Setting up a java project for this would be a chore.
A web server checker
With it installed, I built something small for practice: a check every ten seconds for
whether a web server has died. The code covers:
It does not only check for HTTP 200 — it also reads result_code out of the response
JSON. That was to catch the case where the server is up but internally broken.
Calling req.close() inside finally is deliberate too. The socket has to close even
when an exception is raised.
Running it
1
python ServerCheck.py
Not sure a timer built that way is really okay though…
Summary
On CentOS 6, always use make altinstall. make install breaks yum
Install zlib-devel and openssl-devel first, or pip cannot use SSL
For server checks, read the response body rather than trusting the status code alone