Python Network Programming 简明教程
Python - Network Programming Introduction
随着 Python 作为编程语言的通用性在不断发展,我们发现 Python 也非常适合网络编程领域。随着云计算的发展,网络编程已经成为一个更为热门的话题,Python 在其中扮演了重要角色。以下是将 Python 用作网络编程的首选语言的一些重要原因。
As python’s versatility as a programming language grown over the years, we find that python is very suitable in the world of network programming too. With growth in cloud computing , network programming has become even a more hot topic and python has a big role to play. Below are the few important reasons for python’s use as a preferred language for network Programming.
Socket programming
套接字是客户端和服务器相互通信的链接。例如,当打开一个浏览器时,会自动创建一个套接字来连接到服务器。Python 有一个套接字模块,可用于实现各种套接字功能,例如绑定一个地址或启动一个监听器端口。套接字编程对于计算机网络来说是基础,而 Python 很好地处理了它。
Sockets are the links through which the client and servers communicate with each other. For example when a browser is opened a socket is automatically created to connect with the server. Python has a socket module which an be used to implement various socket functionalities like binding an address or starting a listener port. Socket programming is fundamental to computer networking and python handles it well.
Client programming
客户端是请求信息并等待响应的计算机。可以编写 Python 程序来验证许多客户端操作,例如解析 URL、在提交请求时使用 URL 发送参数、如果访问一个 URL 不成功,连接到另一个 URL 等等。这些程序在客户端程序中运行,即使不使用浏览器也能处理与服务器的所有通信需求。例如,你可以为 Python 程序提供一个 URL 来下载一个文件,它将自己完成此操作,而无需浏览器程序的帮助。
The client is the computer which requests for information and waits for the response. Python programs can be written to validate many client-side actions like parsing a URL, sending parameters with the URL while submitting a request, connect to a alternate URL if access to one URL becomes unsuccessful etc. These programs are run in the client programs and handle all the communication needs with the server even without using a browser. For example – you can provide an URL to the python program for downloading a file and it will get done by the program itself without taking help from the browser program.
Building web servers
使用 Python 作为编程语言可以创建足够简单、能够运行网站的网络服务器。Python 已经有一些内置的网络服务器,可以通过对其进行调整来实现一些所需的其他功能。
It is possible to create simple web servers which are good enough to run websites using python as a programming language. Python already has some inbuilt web servers which can be tweaked to achieve some additional functionalities needed.
SimpleHTTPServer 模块提供了网络服务器的功能,你可以在本地 Python 安装中开始运行它。在 Python 3 中,它被命名为 http.server CherryPy,而 Tornado 是用 Python 编写的网络服务器示例,与像 Apache 或 Ngnix 这样的非 Python 著名网络服务器一样出色。
The SimpleHTTPServer module provides the functionalities of a web server out of the box and you can start running it from the local python installation. In python 3 it is named as http.server*CherryPy* and Tornado are examples of webservers written in python which run as good as non python well known web servers like Apache or Ngnix.
Web Scrapping
Python 闻名的一个重要原因是它在用于网络抓取的语言中占据主导地位。它的数据结构和网络访问能力使其非常适合于访问网页和自动下载数据。如果目标网站提供了一些 API 连接,那么 Python 将通过其程序结构更轻松地处理它。
One of the important reasons python became famous is the its dominance among the languages used for scrapping the web. Its data structure and network access abilities makes it ideal for visiting webpages and download their data automatically. And if there is some API connectivity available for the target website, then python will handle it even more easily through its program structures.
Web Frame works
通过提供预定义的结构和模块化,Web 框架使应用程序开发变得既简单又快捷。开发人员只需很少的编码就可以利用这些现有的库,并进行一些自定义来实现其目标。 Django 和 Flask 是两个著名的框架,即使它们是开源的,但它们也已经得到了大量的商业应用。
Web Frame works makes application development easy and fast by offering pre-defined structures and modularity. The developer has to do minimal coding to leverage those existing libraries and customize a little to achieve the goal. Django and Flask are two famous ones which have seen much commercial use even though they are opensource.
Getting Geo Locations
Python 有用于处理地理数据的库。如果已知纬度和经度,它可以找到营业地址的名称,反之亦然。当然,它需要借助谷歌地图等其他地图提供商的数据。Python 的网络功能真正地拓展到了不同的地理界限!
Python has libraries which handle geographical data. It can find name of the business addresses if the latitude and longitude is known and vice versa. Of course it takes help of other map provider’s data like google maps. Python’s capability for networking truly extends even to different geographic boundaries !