Python核心编程第三版第二章学习笔记
第二章 网络编程
1、学习笔记
2、课后习题
答案是按照自己理解和查阅资料来的,不保证正确性。如由错误欢迎指出,谢谢
1. 套接字:A network socket is an endpoint of a connection across a computer network,Sockets are often represented internally as simple integers, which identify which connection to use.
套接字是网络通信的一个通信端点
有两种类型:
Stream Socket使用TCP协议的面向连接的socket ,SOCK_STREAM
Datagram Socket:使用UDP协议的无连接的socket, SOCK_DGRAM
2. Client/Server 构架
C/S构架
3. TCP和UDP套接字的工作原理
5.使用os.listdir("/etc")返回目录列表,os.curdir 表示当前的目录 .
os.name 返回操作系统信息
#!/usr/bin/python
#Filename : tsUserv.py
from socket import *
from time import ctime
import os
HOST = ""
PORT = 31080
BUFSIZ = 1024
ADDR = (HOST, PORT)
udpSerSock = socket(AF_INET, SOCK_DGRAM)
udpSerSock.bind(ADDR)
while True:
print "waiting for connection"
sysname = os.name
curdir = os.curdir
data, addr = udpSerSock.recvfrom(BUFSIZ)
dir = os.listdir("/etc")
udpSerSock.sendto("[%s] %s %s %s %s" %(ctime(),data,sysname,curdir,dir),addr)
print "received from and retrurned to :",addr
udpSerSock.close()
6.Daytime 服务,获取ssh服务的默认端口号返回给客户端
客户端使用的还是书中的例子,只是改了下服务端口
#!/usr/bin/python
# Filename : 2.6.ser.py
from socket import *
from time import ctime
HOST = "192.168.40.128"
PORT = 40026
ADDR = (HOST,PORT)
BUFSIZ = 1024
udpSerSock = socket(AF_INET,SOCK_DGRAM)
udpSerSock.bind(ADDR)
while True:
print "waiting for messages"
data,addr = udpSerSock.recvfrom(BUFSIZ)
portNum = getservbyname("telnet") #return an integer value ,and the function get the number from the file /etc/services.
# portNum = udpSerSock.getservbyname("ssh")
udpSerSock.sendto("%d"%portNum,addr)
# udpSerSock.sendto("%s"%portNum,addr)
print "received from and returned to:",addr
udpSerSock.close()
7. 半双工聊天
目前实现只能通过client 发起会话,无法从server发起会话,后续考虑如何实现从两侧都能主动发起会话。
server端代码:
#!/usr/bin/python
# Filename : 2.7_server.py
from socket import *
from time import ctime
HOST = "192.168.40.128"
PORT = 40027
BUFSIZ = 10240
ADDR = (HOST,PORT)
tcpSerSock = socket(AF_INET,SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
print "waiting for messages"
while True:
tcpCliSock,addr = tcpSerSock.accept()
print "connected from :",addr
#这里可以实现从两端发起会话,但是会导致一段的消息在另外一端不能及时显示,必须输入要发送的消息之后才能看到上一条对方发送的消息。
# while True:
# data_local = raw_input("waiting for input>>>")
# if not data_local:
# continue
# tcpCliSock.send("[%s] %s"%(ctime(),data_local))
# print "send message to client successful,waiting for response"
while True:
data_remote = tcpCliSock.recv(BUFSIZ)
if not data_remote:
break
print "He says : ",data_remote
while True:
data_local = raw_input("waitint for input>>>")
if not data_local:
continue
tcpCliSock.send("[%s] %s"%(ctime(),data_local))
print "send message to client successful,waitinf for response"
break
tcpSerSock.close()
client端代码:
#!/usr/bin/python
# Filename : 2.7_client.py
from socket import *
from time import ctime
HOST = "192.168.40.128"
PORT = 40027
BUFSIZ = 10240
ADDR = (HOST,PORT)
tcpCliSock = socket(AF_INET,SOCK_STREAM)
tcpCliSock.connect(ADDR)
print "waiting for messages"
while True:
while True:
data_local = raw_input("waiting for input>>>")
if not data_local:
continue
tcpCliSock.send("[%s] %s"%(ctime(),data_local))
print "send message to server successful,waiting for response"
while True:
data_remote = tcpCliSock.recv(BUFSIZ)
if not data_remote:
break
print "He says : ",data_remote
break
tcpCliSock.close()
8.全双工聊天
Python核心编程第三版第二章学习笔记的更多相关文章
- 刷Python核心编程第三版的习题时遇到一个findall的坑
在用正则表达式做以下查找时,发现re.findall()对于正则表达式有没有圆括号是有区分的,具体如下 line = 'Tue Sep 18 12:48:21 2029::ilziuv@zcntzir ...
- AS开发实战第二章学习笔记——其他
第二章学习笔记(1.19-1.22)像素Android支持的像素单位主要有px(像素).in(英寸).mm(毫米).pt(磅,1/72英寸).dp(与设备无关的显示单位).dip(就是dp).sp(用 ...
- #Spring实战第二章学习笔记————装配Bean
Spring实战第二章学习笔记----装配Bean 创建应用对象之间协作关系的行为通常称为装配(wiring).这也是依赖注入(DI)的本质. Spring配置的可选方案 当描述bean如何被装配时, ...
- CSS3秘笈第三版涵盖HTML5学习笔记6~8章
第二部分----CSS实用技术 第6章,文本格式化 指定备用字体: font-family:Arial,Helvetica,sans-serif; 当访问者没有安装第一种字体时,浏览器会在列表中继续往 ...
- CSS3秘笈第三版涵盖HTML5学习笔记1~5章
第一部分----CSS基础知识 第1章,CSS需要的HTML HTML越简单,对搜索引擎越友好 div是块级元素,span是行内元素 <section>标签包含一组相关的内容,就像一本书中 ...
- Linux第一章第二章学习笔记
第一章 Linux内核简介 1.1 Unix的历史 它是现存操作系统中最强大最优秀的系统. 设计简洁,在发布时提供原代码. 所有东西都被当做文件对待. Unix的内核和其他相关软件是用C语言编写而成的 ...
- Machine Learning In Action 第二章学习笔记: kNN算法
本文主要记录<Machine Learning In Action>中第二章的内容.书中以两个具体实例来介绍kNN(k nearest neighbors),分别是: 约会对象预测 手写数 ...
- 《Linux内核设计与实现》课本第一章&第二章学习笔记
<Linux内核设计与实现>课本学习笔记 By20135203齐岳 一.Linux内核简介 Unix内核的特点 Unix很简洁,所提供的系统调用都有很明确的设计目的. Unix中一切皆文件 ...
- Day2 《机器学习》第二章学习笔记
这一章应该算是比价了理论的一章,我有些概率论基础,不过起初有些地方还是没看多大懂.其中有些公式的定义和模型误差的推导应该还是很眼熟的,就是之前在概率论课上提过的,不过有些模糊了,当时课上学得比较浅. ...
随机推荐
- get_post
各种http的请求协议: http://ymiter.iteye.com/blog/1922464 HTTP请求报文和HTTP响应报文 http://www.cnblogs.com/biyeymyhj ...
- 初用protobuf-csharp-port
下面这个用法是参照protobuf-csharp-port的官方wiki,参见: https://code.google.com/p/protobuf-csharp-port/wiki/Getting ...
- canvas三角函数做椭圆运动效果
<canvas id="canvas" width="800" height="400" style="background ...
- [转] java中int,char,string三种类型的相互转换
原文地址:http://blog.csdn.net/lisa0220/article/details/6649707 如何将字串 String 转换成整数 int? int i = Integer.v ...
- 安全框架 SpringSecurity 和 Shiro 对比
突然再次很想理一下权限的事,但是实在不知道实际情况选哪个框架好,现在整理下网上的资料,做一下对比. 1.Spring-security 对spring 结合较好,如果项目用的springmvc ,使用 ...
- lucene-查询query->PhraseQuery多关键字的搜索
用户在搜索引擎中进行搜索时,常常查找的并非是一个简单的单词,很有可能是几个不同的关键字.这些关键字之间要么是紧密相联,成为一个精确的短 语,要么是可能在这几个关键字之间还插有其他无关的关键字.此时,用 ...
- java-读取类中的属性名称和值
方法 /** * 获取类中的所有属性明名称和值(因涉及到可能会是继承关系的父类,所以从f中去属性名称,从f2中取值,两个可以一样,也可以使父类) * @param f:读取属性类(如果取父类的,则这里 ...
- cookie的写入与读出
cookie在jquery中有指定的cookie操作类 $.cookie('the_cookie'); // 读取 cookie $.cookie('the_cookie', 'the_value') ...
- 79.Android之动画基础
转载:http://a.codekk.com/detail/Android/lightSky/%E5%85%AC%E5%85%B1%E6%8A%80%E6%9C%AF%E7%82%B9%E4%B9%8 ...
- Android Studio高级配置
转载:http://www.jianshu.com/p/4243f3b52644 Android Studio 提供了一个非常方便的功能帮助我们导入或者导出设置.因此我们在安装一个新的Androi ...