Python编程之socket

1、TCP

客户端:

from socket import *
ip_port = ("127.0.0.1",8888)
back_log = 5
buffer_size = 1024
tcp_client = socket(AF_INET,SOCK_STREAM)
tcp_client.connect(ip_port)
while 1:
    msg = input(">>>>>>>:").strip()
    if not msg:continue
    tcp_client.send(msg.encode("utf-8"))
    print("客户端已发送消息",msg)
    data = tcp_client.recv(buffer_size)
    print("客户端收到的数据是",data.decode("utf-8"))
tcp_client.close()

服务端:

from socket import *
ip_port = ("127.0.0.1",8888)
back_log = 5
buffer_size = 1024
tcp_server = socket(AF_INET,SOCK_STREAM)
tcp_server.setsockopt(SOL_SOCKET,SO_REUSEADDR,1)#解决服务端断开延时问题
tcp_server.bind(ip_port)
tcp_server.listen(back_log)
print("服务端开始运行了")
while 1:
    conn,adr = tcp_server.accept()#服务端阻塞
    print("双向连接是",conn)
    print("客户端地址是",adr)
    while 1:
        try:
            data = conn.recv(buffer_size)
            print("客户端发来的消息是",data.decode("utf-8"))
            conn.send(data.upper())
            print("服务端回复的消息是",data.upper(),"客户端消息回复成功")
        except Exception:
            break
    conn.close()
tcp_server.close()

 2.UDP

1、服务端

from socket import *
ip_port = ("127.0.0.1",8080)
buffer_size = 1024
udp_server = socket(AF_INET,SOCK_DGRAM)#数据报式套接字
udp_server.bind(ip_port)
while 1:
    data,adr = udp_server.recvfrom(buffer_size)
    print("服务端接受的数据是%s,%s"%(data,adr))#打印接受的数据
    udp_server.sendto(data.upper(),adr)
    print("服务端回复客户端的消息是%s"%data.upper())

2、客户端

from socket import *
ip_port = ("127.0.0.1",8080)
buffer_size = 1024
udp_client = socket(AF_INET,SOCK_DGRAM)#数据报式套接字

while 1:
    msg = input("输入要发的消息:").strip()
    udp_client.sendto(msg.encode("utf-8"),ip_port)
    data,adr = udp_client.recvfrom(buffer_size)
    print("客户端发出的消息是%s"%msg)
    print("客户端接受的数据是%s,%s" % (data, adr))  # 打印接受的数据

 3.NTP

1、客户端

from socket import *
ip_port = ("127.0.0.1",8080)
buffer_size = 1024
udp_client = socket(AF_INET,SOCK_DGRAM)#数据报式套接字

while 1:
    msg = input("输入要发的消息:").strip()
    udp_client.sendto(msg.encode("utf-8"),ip_port)
    data,adr = udp_client.recvfrom(buffer_size)
    print("客户端发出的消息是%s"%msg)
    print("客户端接受的返回的时间是%s,%s" % (data,adr))  # 打印接受的数据

 

2、服务端

from socket import *
ip_port = ("127.0.0.1",8080)
buffer_size = 1024
udp_client = socket(AF_INET,SOCK_DGRAM)#数据报式套接字

while 1:
    msg = input("输入要发的消息:").strip()
    udp_client.sendto(msg.encode("utf-8"),ip_port)
    data,adr = udp_client.recvfrom(buffer_size)
    print("客户端发出的消息是%s"%msg)
    print("客户端接受的返回的时间是%s,%s" % (data,adr))  # 打印接受的数据

 4、多并发服务端

import socketserver
class MyServer(socketserver.BaseRequestHandler):
    def handle(self):
        print(self.request)#coon
        print(self.client_address)#adr
        while 1:
            try:
                #收消息
                data = self.request.recv(1024)
                if not data:
                    break
                print("收到客户端的消息是",data)
                #发消息
                self.request.sendall(data.upper())
            except Exception as e:
                print(e)
                break
if __name__ == '__main__':
    #多线程TCP服务端
    s = socketserver.ThreadingTCPServer(("127.0.0.1",8080),MyServer)
    s.serve_forever()

  

posted @ 2018-12-14 21:26  高母寨寨委书记  阅读(160)  评论(0编辑  收藏  举报