python udp客户端与服务端交互

服务端:

 1 from socket import *
 2 import re
 3 
 4 
 5 udp_socket=socket(AF_INET,SOCK_DGRAM)
 6 udp_socket.bind(('0.0.0.0',8848))
 7 csin=True
 8 while True:
 9     data,addr=udp_socket.recvfrom(1024)
10     if data.decode()=='1':
11         print("客户关闭程序")
12     else:
13         rade = open('dict.txt')
14         for i in rade:
15             word=re.findall(r'(\w+)\s+(.*)',i)
16             print(word)
17             if word[0][0]==data.decode():
18                 print(word)
19                 print(word[0][0])
20                 print(word[0][0]==data.decode())
21                 udp_socket.sendto(word[0][1].encode(), addr)
22 
23 udp_socket.close()

客户端:

 1 from  socket import *
 2 
 3 
 4 
 5 udp_socket=socket(AF_INET,SOCK_DGRAM)
 6 
 7 # text="我进来了,我又出去了,我又进来了,你打我呀,打我呀,笨蛋.".encode()
 8 csin=True
 9 while csin:
10     text=input("客户('1'.退出)>>")
11     if text=='1':
12         udp_socket.sendto(text.encode(),('127.0.0.1',8848))
13         csin=False
14     else:
15         udp_socket.sendto(text.encode(),('127.0.0.1',8848))
16     data,addr=udp_socket.recvfrom(1024)
17     print(data.decode())
18 udp_socket.close()
客户端

服务端处理的内容

a                indef art one

abacus           n.frame with beads that slide along parallel rods, used for teaching numbers to children, and (in some countries) for counting

abandon          v.  go away from (a person or thing or place) not intending to return; forsake; desert

abandonment      n.  abandoning

abase            v. ~ oneself/sb lower oneself/sb in dignity; degrade oneself/sb ;

abash            to destroy the self-possession or self-confidence of:disconcert

abashed          adj. ~ embarrassed; ashamed

abate            v. make or become less

abattoir         n. = slaughterhouse (slaughter)

abbess           n. woman who is head of a convent or nunnery

abbey            n.  buildingin which monks or nuns live as a community under an abbot or abbess

abbot            n. man who is head of a monastery or abbey

abbreviate       v. ~ sth shorten (a word, phrase, etc), esp by omitting letters

abbreviation     n.  abbreviating or being abbreviated

abdicate         v.  resign from or formally renounce the throne

abdication       giving up control, authority

abdomen          n.  part of the body below the chest and diaphragm, containing the stomach, bowels and digestive organs

abdominal        adj. in, of or for the abdomen

abduct           v. take away illegally, using force or deception; kidnap ;

abduction        A carrying away of a person against his will, or illegally.

abed             In bed; on a bed.

aberrant         adj. not following the normal or correct way

aberration       n.  deviation from what is accepted as normal or right

abet             v.  ~ sb (in sth) help or encourage sb to commit an offence or

2020-07-08

 

from  socket import *



udp_socket=socket(AF_INET,SOCK_DGRAM)

# text="我进来了,我又出去了,我又进来了,你打我呀,打我呀,笨蛋.".encode()
csin=True
while csin:
text=input("客户('1'.退出)>>")
if text=='1':
udp_socket.sendto(text.encode(),('127.0.0.1',8848))
csin=False
else:
udp_socket.sendto(text.encode(),('127.0.0.1',8848))
data,addr=udp_socket.recvfrom(1024)
print(data.decode())
udp_socket.close()
posted @ 2020-07-08 20:40  爱吃萝卜爱吃兔  阅读(367)  评论(0编辑  收藏  举报