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.全双工聊天

上一篇:Rest API 开发 学习笔记(转)


下一篇:cglib源码分析(二):Class name 生成策略