Python_Example_pycharm&&socket 网络编程 通讯交互

Author: 楚格 

2018-11-0710:24:02

IDE: Pycharm2018.02   Python 3.7   

KeyWord :  

Explain:  

 思路:

 

1-------------------------------------------

代码实现:

--

客户端:

 1 #
 2 '''
 3 #socket client
 4 '''
 5 
 6 #客户端
 7 import socket
 8 
 9 client = socket.socket() #声明socket类型,同时生成socket连接对象
10 
11 client.connect(('localhost',6969))
12 
13 while True:
14     msg = input(">>:").strip()
15 
16     if len(msg) == 0:
17         print('not message')
18         continue
19 
20     client.send(msg.encode("utf-8"))
21     data = client.recv(10240)
22 
23     print("Receive:",data.decode())
24 
25 client.close()

服务器端

#
'''
# socket _server
'''
#=================================================

#-*-coding:utf-8-*-
#服务器端

import socket

server = socket.socket()
server.bind(('localhost',6969)) #绑定要监听端口
server.listen(5) #监听

print("我要开始等电话了")

while True:
    conn, addr = server.accept()  # 等电话打进来

    # conn就是客户端连过来而在服务器端为其生成的一个连接实例
    print(conn, addr)
    print("电话来了")

    count = 0

    while True:
        data = conn.recv(1024)

        print("recv:",data)

        if not data:
            print("client has lost...")
            break
        conn.send(data.upper())

        count+=1
        if count >10:
            print('count %',count)
            break

server.close()

完*

---

--

转载于:https://www.cnblogs.com/caochucheng/p/9921051.html

你可能感兴趣的:(Python_Example_pycharm&&socket 网络编程 通讯交互)