Foundations of Python 3 Network Programming

c#小王子 c#小王子 2021-04-06 1565 Python,编程,软件


Foundations of Python 3 Network Programming


Thisbook is about network programming with the Python language: about accomplishing a specific set of tasks that all involve a particular technology—computer networks—using a general-purpose programming language that can do all sorts of things besides the things that you will seeillustratedin this book.


We lack the space between the covers of this book to teach you how to programin Python if you have never seen the language before, or never even written a computer program at all.So this book presumes that you have already learned something about Python programming from the many excellent tutorials and bookson the subject.We hope that the Python examplesin the book are good ones, from which you can learn how to structure and write your own Python programs. But we willbeusing all sorts of adyanced Python features without explanation or apology—though, occasionally,we might point out how we areusing a particular technique or construction when we think it is particularly interesting or clever.


On the other hand, this book does not start by assuming that you know any networking!As long as you have ever useda web browseror sent an e-mail, you should know enough to startreading thisbook at the beginning and learn about computer networking along the way. We will approach networking from the point ofview of an application programmer who is either implementing a network-connected service—like a web site, an email server, or a networked computer game—or else writing a client program that is designed to use such a service,


Note that youwillnot, however,learn how to set up orconfigure networks from this book. for the simple reason that the Python language is not usually involved when network engineers or system administrators sit down to build and configure their networks. Instead,computer networks are typically assembled from network switches, Ethernet cables, fiber optic strands, and painstakingly configured routers.You will have tolearn about devices like those from a book that focuses on creating computer networks in the first place; this book instead will talk about writing programs that use acomputer network once it is already set up and running.


The Building Blocks: Stacks and Libraries


As we begin toexplore Python networkprogramming, there are twoconcepts that will appearover and over again:


● The idea of a protocol stack,in which very simple network services are used as a foundation on which to build more sophisticated services.

● The fact that you will often beusing Python libraries of prepared code—whether from the built-in standardlibrary that ships with Python, or from third-party modules that you download and install—that already know how to speak the network protocol you want to use.


In many cases, network programming simply involves selecting andusing a library that already

supports the network operations you need to perform.Amaior purpose of this bookis to introduce vou to all of the key networkinglibraries available for Python, and to teach you about the lower-level network services on which those libraries are built—both so that you understand how the libraries work, and so that you will understand whatis happening when something at a lower level goes wrong.


Let's begin with a very simple example.I have here a mailing address, which lookslike this:


207 N. Defiance St

Archbold, OH


And I am interested in knowing the latitude and longitude of this physical address.It just so happens that Google provides a"Maps API"that can perform such a conversion. What wouldI have to do to take advantage of this network service from Python?


When looking at a new networkservice that you want to use, it is always worthwhile to start by finding out whether someone has alreadyimplemented the protocol—in this case, the Google Maps protocol—that your program willneed to speak. Start by scrolling through the Python StandardLibrary documentation,


But it is important for a Python programmer tolook through the Standard Library's table of contents pretty frequently,even if you usually do not find whatyou are looking for, because each reading will make you more familiar with the services that do come included with Python.


Since the Standard Library does not have a packageto help us, we can turn to the Python Package Index,an excellent resource for finding all sorts of general-purpose Python packages contributed by other programmers andorganizations from across the world. You can also,of course,check theweb site of thevendorwhoseservice you will beusing to see whether they provide a python library to access it. Oryou can do a general Google search for"Python"plus the name ofwhatever web service you want to use,andsee whether any of the first fewresults link to a package that you might want to try.


There,I did a search for Google maps, and immediately found a package thatis actually named googlemaps and that provides a clean interface to its features(though,you will note from its description, it is not vendor-provided, but was instead written by someone besides Google):


This is such acommon situation—that you find a Python packagethat sounds like it might already do exactlywhat you want, and that you want to tryit out on your system—that weshould pausefor a moment and introduce you to the verybest Python technology for quickly trying out a newlibrary: virtualenv!


In the old days, installing a Python package was a gruesome and irreversible act that required administrative privileges on your machine and left your system Python install permanently altered. After several months ofheavy Python development, your system Python install could become a wasteland of dozens ofpackages, all installed by hand, andyou could even find that thenew packages you tried to install would break because they were incompatible with one of the old packages sitting on your hard drive from a project that ended months ago.


Careful Python programmers do not suffer from this situation any longer.Many of us install only one Python package system-wide: virtualenv. Once virtualenvis installed, you have the power to create any number ofsmall, self-contained"virtual Python environments"where packages can be installed, un-installed, and experimented with without contaminatingyour system-wide Python. When a particular project or experiment is over, yousimply remove its virtual environment directory, and your system is clean.


In this case, we want to create a virtual environment in which to test the googlemaps package.


Once you have virtualenv installed, youcan create a newenvironment like this (on Windows, the directorycontaining the Python binaryin the virtualenvironment will be named"Scripts"instead):


$ virtualenv --no-site-packages gmapenv
$ cd gmapenv
$ ls
bin/ include/ lib/
$. bin/activate
$ python -c'import googlemaps' 
Traceback(most recent call last):
  File"<string>",line 1,in <module> 
ImportError:No module named googlemaps


As you can see, the googlemaps package is not yet available! To install it,use the pip commandthat is inside your virtualenv and that is now on your path thanks to the activate command that you ran:


$ pip install googlemaps
Downloading/unpacking googlemaps
  Downloading googlemaps-1.0.2.tar.gz(60Kb): 60Kb downloaded 
  Running setup.py egg info for package googlemaps 
Installing collected packages: googlemaps
  Running setup.py install for googlemaps 
Successfully installed googlemaps 
Cleaning up...


The python binary inside the virtualenv will now have thegooglemaps package available:


$ python -c 'import googlemaps'


Now that youhave the googlemaps package installed,youshould be ableto run thesimple program named search1.py.


Listing 1-1.Fetching a Longitude and Latitude


#!/usr/bin/env python
# Foundations of Python Network Programming- Chapter 1 - search1.py 

from googlemaps import GoogleMaps
address ='207 N. Defiance St,Archbold,OH' 
print GoogleMaps().address_to_latlng(address)


Running it at the command line,you should see a result like this:$ python search1.py(41.5228242,-84.3063479)


【下载地址】

链接:https://pan.baidu.com/s/1inlsGU8f1uFy4X8OpL5iuA

提取码:plpj




文章热度: 166291
文章数量: 333
推荐阅读

FlashFXP绿色版网盘下载,附激活教程 2049

FlashFxp百度网盘下载链接:https://pan.baidu.com/s/1MBQ5gkZY1TCFY8A7fnZCfQ。FlashFxp是功能强大的FTP工具

Adobe Fireworks CS6 Ansifa绿色精简版网盘下载 1763

firework可以制作精美或是可以闪瞎眼的gif,这在广告领域是需要常用的,还有firework制作下logo,一些原创的图片还是很便捷的,而且fireworks用法简单,配合dw在做网站这一块往往会发挥出很强大的效果。百度网盘下载链接:https://pan.baidu.com/s/1fzIZszfy8VX6VzQBM_bdZQ

navicat for mysql中文绿色版网盘下载 1779

Navicat for Mysql是用于Mysql数据库管理的一款图形化管理软件,非常的便捷和好用,可以方便的增删改查数据库、数据表、字段、支持mysql命令,视图等等。百度网盘下载链接:https://pan.baidu.com/s/1T_tlgxzdQLtDr9TzptoWQw 提取码:y2yq

火车头采集器(旗舰版)绿色版网盘下载 1888

火车头采集器是站长常用的工具,相比于八爪鱼,简洁好用,易于配置。火车头能够轻松的抓取网页内容,并通过自带的工具对内容进行处理。站长圈想要做网站,火车头采集器是必不可少的。百度网盘链接:https://pan.baidu.com/s/1u8wUqS901HgOmucMBBOvEA

Photoshop(CS-2015-2023)绿色中文版软件下载 2002

安装文件清单(共46G)包含Window和Mac OS各个版本的安装包,从cs到cc,从绿色版到破解版,从安装文件激活工具,应有尽有,一次性打包。 Photoshop CC绿色精简版 Photoshop CS6 Mac版 Photoshop CC 2015 32位 Photoshop CC 2015 64位 Photoshop CC 2015 MAC版 Photoshop CC 2017 64位 Adobe Photoshop CC 2018 Adobe_Photoshop_CC_2018 Photoshop CC 2018 Win32 Photoshop CC 2018 Win64

知之

知之平台是全球领先的知识付费平台。提供各个领域的项目实战经验分享,提供优质的行业解决方案信息,来帮助您的工作和学习

使用指南 建议意见 用户协议 友情链接 隐私政策 Powered by NOOU ©2020 知之