码迷,mamicode.com
首页 > 编程语言 > 详细

Python--网络编程-----socket代码实例

时间:2018-04-12 00:13:19      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:实例   localhost   循环   cep   server   学习python   connect   cli   网络编程   

一、基本socket例子

服务端:

 1 import socket
 2 
 3 HOST = ‘‘
 4 PORT = 50007
 5 
 6 sock_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 7 sock_server.bind((HOST, PORT))
 8 
 9 sock_server.listen(1)
10 conn, addr = sock_server.accept()
11 
12 with conn:
13     print(Connected by, addr)
14     while True:
15         data = conn.recv(1024)
16         if not data:
17             break
18         conn.sendall(data)

客户端:

 1 import socket
 2 
 3 HOST = localhost
 4 PORT = 50007
 5 
 6 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 7 client.connect((HOST, PORT))
 8 client.sendall(bHello, world)
 9 
10 data = client.recv(1024)
11 
12 print(Received, data)

先运行服务端,再运行客户端,客户端运行结果为:

1 Received bHello, world

二、循环收发数据

服务端:

 1 import socket
 2 
 3 HOST = ‘‘
 4 PORT = 50007
 5 
 6 sock_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 7 sock_server.bind((HOST, PORT))
 8 
 9 sock_server.listen(1)
10 conn, addr = sock_server.accept()
11 
12 with conn:
13     print(Connected by, addr)
14     while True:
15         data = conn.recv(1024)
16         print("server recv:", conn.getpeername(), data.decode())
17         if not data:
18             break
19         conn.sendall(data)

客户端:

 1 import socket
 2 
 3 HOST = localhost
 4 PORT = 50007
 5 
 6 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 7 client.connect((HOST, PORT))
 8 
 9 while True:
10     msg = input(">>>:").strip()
11     if len(msg) == 0:
12         continue
13     client.sendall(msg.encode())
14 
15     data = client.recv(1024)
16 
17     print(Received, data.decode())

先运行服务端,在运行客户端,依次在客户端输入:

hi、你好、学习python,

客户端运行结果为:

1 >>>:hi
2 Received hi
3 >>>:你好
4 Received 你好
5 >>>:学习python
6 Received 学习python
7 >>>:

服务端运行结果为:

1 Connected by (127.0.0.1, 57473)
2 server recv: (127.0.0.1, 57473) hi
3 server recv: (127.0.0.1, 57473) 你好
4 server recv: (127.0.0.1, 57473) 学习python

三、简单聊天软件

服务端

 1 import socket
 2 
 3 HOST = ‘‘
 4 PORT = 50007
 5 
 6 sock_server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 7 sock_server.bind((HOST, PORT))
 8 
 9 sock_server.listen(1)
10 conn, addr = sock_server.accept()
11 
12 with conn:
13     print(Connected by, addr)
14     while True:
15         data = conn.recv(1024)
16         print("server recv:", conn.getpeername(), data.decode())
17         if not data:
18             break
19 
20         response = input(">>>:").strip()
21         conn.send(response.encode())
22         print("send to alex:", response)

客户端

 1 import socket
 2 
 3 HOST = localhost
 4 PORT = 50007
 5 
 6 client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 7 client.connect((HOST, PORT))
 8 
 9 while True:
10     msg = input(">>>:").strip()
11     if len(msg) == 0:
12         continue
13     client.sendall(msg.encode())
14 
15     data = client.recv(1024)
16 
17     print(Received, data.decode())

先运行服务端,再运行客户端,

客户端运行结果为:

1 >>>:Hi
2 Received hey
3 >>>:刚强
4 Received alex
5 >>>:egon在吗
6 Received 不在
7 >>>:哈哈
8 Received 哈哈
9 >>>:

服务端运行结果为:

 1 Connected by (127.0.0.1, 57682)
 2 server recv: (127.0.0.1, 57682) Hi
 3 >>>:hey
 4 send to alex: hey
 5 server recv: (127.0.0.1, 57682) 刚强
 6 >>>:alex
 7 send to alex: alex
 8 server recv: (127.0.0.1, 57682) egon在吗
 9 >>>:不在
10 send to alex: 不在
11 server recv: (127.0.0.1, 57682) 哈哈
12 >>>:哈哈
13 send to alex: 哈哈

可以看出吗,以上代码能够实现简单的聊天功能

 

Python--网络编程-----socket代码实例

标签:实例   localhost   循环   cep   server   学习python   connect   cli   网络编程   

原文地址:https://www.cnblogs.com/xudachen/p/8799271.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!