2-5 网络互连和套接字
先运行服务器,然后启动客户端,客户端可输入任意指令,若输入了date
,os
,ls
之一,则会相应从服务器返回时间戳、当前操作系统信息,当前目录文件清单
echoserv.py
# coding=utf-8
# TCP服务器程序
import socket
from time import ctime
import os
HOST = ''
PORT = 50007
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.bind((HOST,PORT))
s.listen(1)
conn, addr = s.accept()
msg = b''
with conn:
print('Connected by',addr)
while True:
data = conn.recv(1024)
if not data:break
print('Received ',data)
if data == b'date':
msg = bytes('[%s]' % ctime(), 'utf-8')
elif data == b'os':
msg = bytes('[%s]' % os.name,'utf-8')
elif data == b'ls':
msg = bytes('[%s]' % os.listdir(),'utf-8')
else:
msg = data
conn.sendall(msg)
echoclnt.py
# coding=utf-8
# TCP客户端程序
import socket
HOST = 'localhost'
PORT = 50007
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
s.connect((HOST,PORT))
data=input('> ')
s.sendall(bytes(data,'utf-8'))
data = s.recv(1024)
print('Received',data.decode('utf-8'))
2-6 Daytime服务
得到端口号应该是13
getservbyname("daytime","udp")
udpserv.py
# coding=utf-8
# UDP服务器程序
from socket import *
from time import ctime
HOST = ''
# 确定使用UDP协议的“daytime”服务的端口号
PORT=getservbyname("daytime","udp")
print('使用UDP协议的“daytime”服务的端口号为%s' % PORT)
BUFSIZ = 1024
ADDR = (HOST, PORT)
# udpSerSock = socket(AF_INET, SOCK_STREAM)
udpSerSock = socket(AF_INET, SOCK_DGRAM)
udpSerSock.bind(ADDR)
try:
while True:
print('waiting for message...')
data, addr = udpSerSock.recvfrom(BUFSIZ)
if not data:
break
udpSerSock.sendto(bytes('[%s] %s' % (ctime(),data.decode('utf-8')),'utf-8'),addr)
print('...received from and returned to:', addr)
except Exception as err:
print("[UDP_SERVE_ERROR] " + str(err))
finally:
udpSerSock.close()
udpclnt.py
# coding=utf-8
# UDP客户端
from socket import *
HOST = 'localhost'
# 确定使用UDP协议的“daytime”服务的端口号
PORT=getservbyname("daytime","udp")
print('使用UDP协议的“daytime”服务的端口号为%s' % PORT)
BUFSIZ = 1024
ADDR = (HOST, PORT)
udpCliSock = socket(AF_INET,SOCK_DGRAM)
try:
while True:
data = input('> ')
if not data:
break
udpCliSock.sendto(bytes(data,'utf-8'),ADDR)
data,ADDR = udpCliSock.recvfrom(BUFSIZ)
print(data.decode('utf-8'))
except EOFError as err:
print("[UDP_CLIENT_ERROR] " + str(err))
finally:
udpCliSock.close()
2-7 半双工聊天
tcpserv.py
# coding=utf-8
# TCP半双工聊天服务器程序
from socket import *
from time import ctime
HOST = ''
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpSerSock = socket(AF_INET, SOCK_STREAM)
tcpSerSock.bind(ADDR)
tcpSerSock.listen(5)
while True:
tcpCliSock, addr = tcpSerSock.accept()
while True:
#接收消息
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
print('client> ',data.decode('utf-8'))
#发送消息
msg = input('server> ')
tcpCliSock.send(bytes(msg, 'utf-8'))
tcpCliSock.close()
tcpSerSock.close()
tcpclnt.py
# coding=utf-8
# TCP半双工聊天客户端程序
from socket import *
HOST = '127.0.0.1'
PORT = 21567
BUFSIZ = 1024
ADDR = (HOST, PORT)
tcpCliSock = socket(AF_INET, SOCK_STREAM)
tcpCliSock.connect(ADDR)
while True:
# 发送消息
data = input('client> ')
if not data:
break
tcpCliSock.send(bytes(data, 'utf-8'))
# 接收消息
data = tcpCliSock.recv(BUFSIZ)
if not data:
break
print('server> ',data.decode('utf-8'))
tcpCliSock.close()
效果图
客户端服务端
网友评论