The first chapter 1.19 Network Programming Fundamentals

A. Socket programming

  • socket called Socket
  • socket programming is actually performed using the code to implement the two-side network communications; socket communications program is implemented two
  • Two ends communicate into the server and the client two kinds of
  • python through the socket module providing classes and methods associated socket programming

II. Server-side

(Note: the following server and client implemented in two files py)

1. Create a socket object (buy phone)

socket(family=AF_INET, type=SOCK_STREAM)

family - 设置ip类型; AF_INET对应的是ipv4; AF_INET6对应的是ipv6
type - 设置传输类型;  SOCK_STREAM对应的是TCP协议; SOCK_DGRAM对应的是UDP协议

2. Binding IP and port (plug telephone line)

bind((ip地址, 端口))

ip地址: 找到互联网中唯一的一台计算机; 赋值ip地址对应的字符串
端口: 区分同一台计算机中不同的服务(程序); 赋整数,值的范围是0~65535, 其中0~1024属于著名,不能随便用。
     同一时间同一个端口只能对应一个服务

3. Start listening (and other phones)

server.listen(N)

N : 表示能同时接通的"电话"的数量

4. accept client requests (phone)

connection, address = server.accept()

connection : 服务器接收的客户端的分机对象
address : 该客户端的IP地址
返回为这个客户端创建的独立的套接字对象(分机)和客户端的地址
当程序运行到这句代码的时候会停下来,直到有请求为止

5. receive messages (listen to each other talk)

分机对象 . recv(一次性能够接收的数据的大小)

返回接收到的数据, 数据类型是二进制

6. Send the message (listen to others speak)

分机对象.send(需要发送的数据)

7. close the connection (hang up)

分机对象.close()


A complete server-side

from socket import socket

server = socket()
server.bind(('192.168.10.234', 5200)) # ip地址可以用自己电脑的IP, 端口随便,只要不是0-1024
server.listen(50) # 表示这个服务器可以同时和50个客户端进行通信
while True: # 保证通话一直进行
    print('正在监听...')
    connection, address = server.accept() # 接受客户端请求,并创建分机对象connection, 返回分机地址address
    recv_data = connection.recv(1024).decode(encoding='utf-8') # 接收客户端发来的数据,并将二进制转换成字符串(.decode方法)
    print('recv_data') # 打印接收到的数据
    massage = input('请输入发送内容:') # 输入要发送的内容
    connection.send(massage) # 发送数据
    connection.close() # 关闭对话

III. The client

1. Create a socket object (buy phone)

client = socket()

2. Connect server

client.connect(('192.168.10.234', 5200))

3. Send Message

client.send()

4. Receiving message

client.recv()

5. Disconnect

client.close()

A complete client

client = socket()
client.connect(('192.168.10.234', 5200))
client.send('服务器你好吗?'.encode())
re_data = client.recv(1024)
print(re_data.decode(encoding='utf-8'))
client.close()

IV. Other Sao operations

1. The one constant communication

"""服务器端"""
from socket import socket

server = socket()
server.bind(('10.7.156.55', 9999))
server.listen(20)
while True:
    print('正在监听...')
    connection, address = server.accept()
    # 持续通信
    while True:
        re_data = connection.recv(1024)
        re_massage = re_data.decode(encoding='utf-8')
        print('client:', re_massage)
        if re_massage == '再见':
            connection.close()
            break

        massage = input('server:')
        connection.send(massage.encode())
        if massage == '再见':
            connection.close()
            break


"""客户端"""
from socket import socket

client = socket()
client.connect(('10.7.156.55', 9999))
while True:
    massage = input('client:')
    # for _ in range(10):
    client.send(massage.encode())
    if massage == '拜拜':
        client.close()
        break

    re_massage = client.recv(1024).decode(encoding='utf-8')
    print('server:', re_massage)
    if re_massage == '拜拜':
        client.close()
        break

2. Send pictures

"""服务器端"""
from socket import socket

server = socket()
server.bind(('10.7.156.55', 8000))
server.listen(1024)
while True:
    connection, address = server.accept()
    with open('../code/1024x1024.jpg', 'rb') as f:
        data = f.read()
    connection.send(data)


"""客户端"""
from socket import socket

client = socket()
client.connect(('10.7.156.55', 8000))
# 接收图片长度
total_length = int(client.recv(1024).decode())
print('第一次:', total_length)
sum_data = bytes()   # 保存接收到的图片的总数据
# 接收图片
while True:
    re_data = client.recv(1024)
    sum_data += re_data
    if len(sum_data) == total_length:
        with open('client/test1.wav', 'wb') as f:
            f.write(sum_data)
        break

End Sahua

Guess you like

Origin www.cnblogs.com/anjhon/p/11960722.html