zoukankan      html  css  js  c++  java
  • socket套接字小荔枝

    通信循环 客户端

    import socket

    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

    phone.connect(("127.0.0.1",8066))

    import time

    time.sleep(3)

    while True:

        msg=input("input your msg >>:").strip()

         if msg=="q":break

        if len(msg)==0:continue

        phone.send(msg.encode("utf-8"))

        data=phone.recv(1024)

        print(data.decode("utf-8"))

    phone.close()

    通信循环服务端

    import socket

    phone=socket.socket(socket.AF_INET,socket.SOCK_STREAM)

    phone.bind(("127.0.0.1",8066))

    phone.listen(5)

    print("服务端正在监听:%s:%s"%("127.0.0.1",8066))

    conn,client_addr=phone.accept()

    print("客户端IP和端口为:",client_addr)

    while True:

        try:

            data=conn.recv(1024)

            if len(data)==0:break

            print("客户端发送的消息为:",data.decode("utf-8"))

            conn.send(data.upper())

        except Exception:

            break

    conn.close()

    phone.close()

  • 相关阅读:
    Daily Scrum 10.31
    Daily Scrum 10.30
    Daily Scrum 10.29
    Daily Scrum 10.28
    扩展设置
    安装
    james+javamail入门
    解决ZF2_PATH environment
    Apache Httpd Server 2.2升级2.4
    缓存,spring
  • 原文地址:https://www.cnblogs.com/mayrain/p/12741073.html
Copyright © 2011-2022 走看看