顾名思义,也算是网络编程的作业吧
一步步来,第一步,搭建一个UDP通信(便于模拟,选择了本地服务),实现个小功能好了,服务器将收到的字符全部转换成大写发回给客户端
服务器:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#服务器
import socket
BUFSIZE = 1024 #规定缓冲区的大小
ip_port = ('127.0.0.1', 9999)
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # udp协议
server.bind(ip_port)
print('Listening at {}'.format(server.getsockname)) #getsockname()可以获取套接字目前绑定的IP地址即端口,返回的是一个二元组
while True:
data,client_addr = server.recvfrom(BUFSIZE) #从缓冲区读取1024个字节,The return value is a pair (bytes, client_address)
if data==b'QUIT':
server.sendto(b'see you later',client_addr)
continue
print('Data recived from clinet:', data)
server.sendto(data.upper(),client_addr) #将收到的数据转换成大写字母发回给clinet
server.close()
客户端:
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import socket
BUFSIZE = 1024
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # udp协议
while True:
msg = input(">> ").strip() #strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
ip_port = ('127.0.0.1', 9999)
client.sendto(msg.encode('utf-8'),ip_port) # 以utf-8编码对u进行编码,获得bytes类型对象
data,server_addr = client.recvfrom(BUFSIZE)
if data==b'see you later': #输入QUIT即可断开连接
print('Data recived from sever:',data,server_addr)
#client.sendto(b'goodbye',ip_port)
break
print('Data recived from sever:',data,server_addr)
client.close()
然后就是模拟一个字典查询了,我比较才,不会搭建数据库什么的,我就直接从本地的txt里面读取保存着的字典内容来实现,菜到哭泣
读取字典
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
def searchtext(str):
f = open("新东方红宝书.txt",'rb') #因为读取的不全是unicode编码,比如读取中文是就会出现乱码,只能读取字节流
try:
#f = open('新东方红宝书.txt','r')
line = f.readline().strip()
result=line.find(str)
if result>=0:
print(line.decode('gb2312','ignore'))
return
while line:
line = f.readline().strip()
result=line.find(str)
if result>=0:
print(line.decode('gb2312','ignore'))
return
finally:
if f:
f.close()
return
if __name__ == '__main__':
searchtext(b'abhorrent')
结果如下
接下来就是合起来试试看了
server
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import string
import socket
def searchtext(str):
f = open("新东方红宝书.txt",'rb') #因为读取的不全是unicode编码,比如读取中文是就会出现乱码,只能读取字节流
try:
#f = open('新东方红宝书.txt','r')
line = f.readline().strip()
result=line.find(str)
if result>=0:
print(line.decode('gb2312','ignore'))
return line
while line:
line = f.readline().strip()
result=line.find(str)
if result>=0:
print(line.decode('gb2312','ignore'))
return line
finally:
if f:
f.close()
return
BUFSIZE = 1024 #规定缓冲区的大小
ip_port = ('127.0.0.1', 9999)
server = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # udp协议
server.bind(ip_port)
print('Listening at {}'.format(server.getsockname)) #getsockname()可以获取套接字目前绑定的IP地址即端口,返回的是一个二元组
while True:
data,client_addr = server.recvfrom(BUFSIZE) #从缓冲区读取1024个字节,The return value is a pair (bytes, client_address)
if data==b'QUIT':
server.sendto(b'see you later',client_addr)
continue
print('Data recived from clinet:', data)
server.sendto(searchtext(data),client_addr)
#server.sendto(data.upper(),client_addr) #将收到的数据转换成大写字母发回给clinet
server.close()
client
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import socket
BUFSIZE = 1024
client = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # udp协议
while True:
msg = input(">> ").strip() #strip() 方法用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
ip_port = ('127.0.0.1', 9999)
client.sendto(msg.encode('utf-8'),ip_port) # 以utf-8编码对u进行编码,获得bytes类型对象
data,server_addr = client.recvfrom(BUFSIZE)
if data==b'see you later': #输入QUIT即可断开连接
print('Data recived from sever:',data,server_addr)
#client.sendto(b'goodbye',ip_port)
break
print('Data recived from sever:',data.decode('gb2312','ignore'),server_addr)
client.close()
实验结果
当然后续还是会加一些功能的,什么没有搜查到什么的,比较想做一个切换字典的功能