Python Network Programming 简明教程

Python - Network Environment

Python 3 可用于 Windows、Mac OS 以及大多数版本的 Linux 操作系统。即使 Python 2 可用于许多其他操作系统,但 Python 3 的支持尚未向它们提供,或者已停止。

Python 3 is available for Windows, Mac OS and most of the flavors of Linux operating system. Even though Python 2 is available for many other OSs, Python 3 support either has not been made available for them or has been dropped.

Local Environment Setup

打开一个终端窗口并输入“python”以找出它是否已经安装,以及安装的是哪个版本。

Open a terminal window and type "python" to find out if it is already installed and which version is installed.

Getting Python

Windows platform

Python 3 的最新版本(Python 3.5.1)的二进制文件可以在 this download page 上获得

Binaries of latest version of Python 3 (Python 3.5.1) are available on this download page

可用的不同安装选项有以下几种。

The following different installation options are available.

  1. Windows x86-64 embeddable zip file

  2. Windows x86-64 executable installer

  3. Windows x86-64 web-based installer

  4. Windows x86 embeddable zip file

  5. Windows x86 executable installer

  6. Windows x86 web-based installer

Note − 要安装 Python 3.5.1,最低操作系统要求是 Windows 7 带 SP1。对于版本 3.0 到 3.4.x,Windows XP 是可以接受的。

Note − In order to install Python 3.5.1, minimum OS requirements are Windows 7 with SP1. For versions 3.0 to 3.4.x Windows XP is acceptable.

Linux platform

不同版本的 Linux 使用不同的软件包管理器来安装新软件包。

Different flavors of Linux use different package managers for installation of new packages.

在 Ubuntu Linux 中,Python 3 使用以下命令从终端中安装。

On Ubuntu Linux, Python 3 is installed using the following command from the terminal.

$sudo apt-get install python3-minimal

从源代码安装

Installation from source

从 Python 的下载 URL 下载 gzip 源 tar 包 − https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz

Download Gzipped source tarball from Python’s download URL − https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz

Extract the tarball
tar xvfz Python-3.5.1.tgz
Configure and Install:
cd Python-3.5.1
./configure --prefix = /opt/python3.5.1
make
sudo make install

Mac OS

从该 URL 下载 Mac OS 安装程序 − https://www.python.org/downloads/mac-osx/

Download Mac OS installers from this URL − https://www.python.org/downloads/mac-osx/

  1. Mac OS X 64-bit/32-bit installer − python-3.5.1-macosx10.6.pkg

  2. Mac OS X 32-bit i386/PPC installer − python-3.5.1-macosx10.5.pkg

双击此软件包文件并按照向导说明进行安装。

Double click this package file and follow the wizard instructions to install.

在 Python 官方网站上可获得最新的源代码、二进制文件、文档、新闻等 −

The most up-to-date and current source code, binaries, documentation, news, etc., is available on the official website of Python −

Python Official Websitehttps://www.python.org/

Python Official Websitehttps://www.python.org/

您可以从以下网站下载 Python 文档。文档以 HTML、PDF 和 PostScript 格式提供。

You can download Python documentation from the following site. The documentation is available in HTML, PDF and PostScript formats.

Python Documentation Websitewww.python.org/doc/

Python Documentation Websitewww.python.org/doc/

Setting up PATH

程序和其他可执行文件可以位于多个目录中。因此,操作系统会提供一个搜索路径,其中列出了它搜索可执行文件所在的目录。

Programs and other executable files can be in many directories. Hence, the operating systems provide a search path that lists the directories that it searches for executables.

其重要特性有 −

The important features are −

  1. The path is stored in an environment variable, which is a named string maintained by the operating system. This variable contains information available to the command shell and other programs.

  2. The path variable is named as PATH in Unix or Path in Windows (Unix is case-sensitive; Windows is not).

  3. In Mac OS, the installer handles the path details. To invoke the Python interpreter from any particular directory, you must add the Python directory to your path.

Setting Path at Unix/Linux

要在 Unix 中为特定会话将 Python 目录添加到路径中 −

To add the Python directory to the path for a particular session in Unix −

  1. In the csh shell − type setenv PATH "$PATH:/usr/local/bin/python3" and press Enter.

  2. In the bash shell (Linux) − type export PYTHONPATH=/usr/local/bin/python3.4 and press Enter.

  3. In the sh or ksh shell − type PATH="$PATH:/usr/local/bin/python3" and press Enter.

*注 − * /usr/local/bin/python3 是 Python 目录的路径。

*Note − * /usr/local/bin/python3 is the path of the Python directory.

Setting Path at Windows

要在Windows特定会话的路径中添加Python目录:

To add the Python directory to the path for a particular session in Windows −

  1. At the command prompt − type path %path%;C:\Python and press Enter.

Note - C:\Python是Python目录的路径

Note − C:\Python is the path of the Python directory

Running Python

有三种不同的方法可用于启动Python:

There are three different ways to start Python −

Interactive Interpreter

您可以从Unix、DOS或任何其他提供命令行解释器或shell窗口的系统启动Python。

You can start Python from Unix, DOS, or any other system that provides you a command-line interpreter or shell window.

在命令行中输入 python

Enter python the command line.

在交互式解释器中立即开始编码。

Start coding right away in the interactive interpreter.

$python             # Unix/Linux

or

python%             # Unix/Linux

or

C:>python           # Windows/DOS

Integrated Development Environment

如果系统上的 GUI 应用程序支持 Python,您还可以从图形用户界面 (GUI) 环境中运行 Python。

You can run Python from a Graphical User Interface (GUI) environment as well, if you have a GUI application on your system that supports Python.

  1. Unix − IDLE is the very first Unix IDE for Python.

  2. WindowsPythonWin is the first Windows interface for Python and is an IDE with a GUI.

  3. Macintosh − The Macintosh version of Python along with the IDLE IDE is available from the main website, downloadable as either MacBinary or BinHex’d files.

如果无法正确设置环境,那么可以寻求系统管理员帮助。确保 Python 环境正确设置且正常工作。

If you are not able to set up the environment properly, then you can take the help of your system admin. Make sure the Python environment is properly set up and working perfectly fine.

Note − 后续章节中给出的所有示例都是使用 Windows 7 和 Ubuntu Linux 上的 Python 3.4.1 版本执行的。

Note − All the examples given in subsequent chapters are executed with Python 3.4.1 version available on Windows 7 and Ubuntu Linux.

我们已经在线设置了 Python 编程环境,以便在学习理论时在线执行所有可用示例。随意修改任意示例并在线执行它。

We have already set up Python Programming environment online, so that you can execute all the available examples online while you are learning theory. Feel free to modify any example and execute it online.