Home  >  Article  >  Backend Development  >  Issues related to socket sockets in python3

Issues related to socket sockets in python3

零下一度
零下一度Original
2017-07-02 10:37:271663browse

This article mainly introduces the detailed solution to the encoding problem of socket socket in python3. It has certain reference value. Interested friends can refer to it

1. TCP

1. Create tcp server

#创建服务器
from socket import *
from time import ctime #导入ctime

HOST = ''    #任意主机
PORT = 21567  #随机提供个端口号
BUFSIZ = 1024  # 缓冲区大小设置为1KB,可以根据网络性能和程序需要改变这个容量
ADDR = (HOST, PORT)

tcpSerSock = socket(AF_INET, SOCK_STREAM)  #分配了 TCP 服务器套接字
tcpSerSock.bind(ADDR)  #绑定到服务器地址以及开启 TCP 监听器的调用。
tcpSerSock.listen(5)  #listen()方法的参数是在连接被转接或拒绝之前,传入连接请求的最大数

"""
  一旦进入服务器的无限循环之中,我们就(被动地)等待客户端的连接。当一个连接请求出
现时,我们进入对话循环中,在该循环中我们等待客户端发送的消息。如果消息是空白的,这意
味着客户端已经退出,所以此时我们将跳出对话循环,关闭当前客户端连接,然后等待另一个客
户端连接。如果确实得到了客户端发送的消息,就将其格式化并返回相同的数据,但是会在这些
数据中加上当前时间戳的前缀。最后一行永远不会执行,它只是用来提醒读者,如果写了一个处
理程序来考虑一个更加优雅的退出方式,正如前面讨论的,那么应该调用 close()方法。
"""
while True:
  print("waiting for connection")
  tcpCliSock, addr = tcpSerSock.accept() # 接收客户端连接,返回客户端和地址
  print("...connected from:", addr)

  while True:
    data = tcpCliSock.recv(BUFSIZ).decode() #对话(接收 / 发送) 接收客户端的data
    if not data:
      break
    tcpCliSock.send(('service:'+ctime()+'--'+data).encode())  #发送时间戳 和data信息给客户端

  tcpCliSock.close()
tcpSerSock.close()

2. Create tcp client


from socket import *

HOST = 'localhost'
PORT = 21567      #端口号 PORT 应该与你为服务器设置的完全相同(否则,将无法进行通信)
BUFSIZ = 1024
ADDR = (HOST, PORT)

tcpCliSock = socket(AF_INET, SOCK_STREAM)  #分配 TCP 客户端套接字
tcpCliSock.connect(ADDR)  #主动连接

"""
  我们必须解码来自服务器端的字符串(借助于distutils.log.warn()

"""
while True:
  data = input("> ")
  if not data:   #用户如果没有输入,则终止
    break
  tcpCliSock.send(data.encode())   #发送客户端的data给服务器
  data = tcpCliSock.recv(BUFSIZ).decode() #接收服务器的data
  if not data:  #或者服务器终止且对 recv()方法的调用失败
    break
  print('返回:%s'%data)
tcpCliSock.close()

2. UDP

1. UDP server creation

"""
  这个脚本创建一个 UDP 服务器,它接受客户端发来的消息,并将加了时间戳前缀的该消息返回给客户端。
"""
from socket import *

from time import ctime
HOST = ""

PORT = 21567

BUFSIZ = 1024

ADDR = (HOST, PORT)
udpServer = socket(AF_INET, SOCK_DGRAM)

udpServer.bind(ADDR)
while True:
  print("waiting for masssage")
  data,addr = udpServer.recvfrom(BUFSIZ) #接收

  data = data.decode()

  udpServer.sendto((ctime()+"--"+ data).encode(),addr)

  print("received from and returned to ",addr)
udpServer.close()

2. UDP client creation


##

#这个脚本创建一个 UDP 客户端,它提示用户输入发送给服务器的消息,并接收服务器加了时间戳前缀的消息,然后将它们显示给用户。
from socket import *
HOST = 'localhost'
PORT = 21567      #端口号 PORT 应该与你为服务器设置的完全相同(否则,将无法进行通信)
BUFSIZ = 1024
ADDR = (HOST, PORT)

udpCS = socket(AF_INET, SOCK_DGRAM)

while True:
  data = input("> ")
  if not data:
    break
  udpCS.sendto(data.encode(),ADDR)
  data,ADDR = udpCS.recvfrom(BUFSIZ)
  if not data:
    break
  print(data)

udpCS.close()

In fact, the principles are similar. In python3, the contents transmitted using sockets are Transmitted in byte form, encoding is required when transmitting (send/sendto), and decoding is required when receiving (recv). As long as you grasp this key point, dealing with this problem is very simple.

The above is the detailed content of Issues related to socket sockets in python3. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn