Python Socket深度学习分享

简介: Python Socket深度学习分享

Server:

复制代码
import socket
port=8081
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

从指定的端口,从任何发送者,接收UDP数据

s.bind(('',port))
print('正在等待接入...')
while True:

#接收一个数据
data,addr=s.recvfrom(1024)
print('Received:',data,'from',addr)

复制代码
Client:

import socket
port=8081
host='localhost'
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
s.sendto(b'hello,this is a test info !',(host,port))

很简单 。下面是TCP方式:

Server:

复制代码

-- coding: utf-8 --

from socket import *
from time import ctime

//代码效果参考:https://v.youku.com/v_show/id_XNjQwNjgzODMwNA==.html

HOST=''
PORT=12345
BUFSIZ=1024
ADDR=(HOST, PORT)
sock=socket(AF_INET, SOCK_STREAM)

sock.bind(ADDR)

sock.listen(5)
while True:
print('waiting for connection')
tcpClientSock, addr=sock.accept()
print('connect from ', addr)
while True:
try:
data=tcpClientSock.recv(BUFSIZ)
except:
print(e)
tcpClientSock.close()
break
if not data:
break
s='Hi,you send me :[%s] %s' %(ctime(), data.decode('utf8'))
tcpClientSock.send(s.encode('utf8'))
print([ctime()], ':', data.decode('utf8'))
tcpClientSock.close()
sock.close()
复制代码
Client:

复制代码

-- coding: utf-8 --

from socket import *

class TcpClient:
HOST='127.0.0.1'
PORT=12345
BUFSIZ=1024
ADDR=(HOST, PORT)
def init(self):
self.client=socket(AF_INET, SOCK_STREAM)
self.client.connect(self.ADDR)

    while True:
        data=input('>')
        if not data:
            break
        self.client.send(data.encode('utf8'))
        data=self.client.recv(self.BUFSIZ)
        if not data:
            break
        print(data.decode('utf8'))

//代码效果参考:https://v.youku.com/v_show/id_XNjQwNjg0MTE5Mg==.html

if name == 'main':
client=TcpClient()
复制代码
上面的TCP方式有个问题,不能退出,好吧,我们改造一下,使这个程序可以发送quit命令以退出:

Server:

复制代码

-- coding: utf-8 --

from socket import *
from time import ctime
from time import localtime
import time

HOST=''
PORT=1122 #设置侦听端口
BUFSIZ=1024
ADDR=(HOST, PORT)
sock=socket(AF_INET, SOCK_STREAM)

sock.bind(ADDR)

sock.listen(5)

设置退出条件

STOP_CHAT=False
while not STOP_CHAT:
print('等待接入,侦听端口:%d' % (PORT))
tcpClientSock, addr=sock.accept()
print('接受连接,客户端地址:',addr)
while True:
try:
data=tcpClientSock.recv(BUFSIZ)
except:

        #print(e)
        tcpClientSock.close()
        break
    if not data:
        break
    #python3使用bytes,所以要进行编码
    #s='%s发送给我的信息是:[%s] %s' %(addr[0],ctime(), data.decode('utf8'))
    #对日期进行一下格式化
    ISOTIMEFORMAT='%Y-%m-%d %X'
    stime=time.strftime(ISOTIMEFORMAT, localtime())
    s='%s发送给我的信息是:%s' %(addr[0],data.decode('utf8'))
    tcpClientSock.send(s.encode('utf8'))
    print([stime], ':', data.decode('utf8'))
    #如果输入quit(忽略大小写),则程序退出
    STOP_CHAT=(data.decode('utf8').upper()=="QUIT")
    if STOP_CHAT:
        break

tcpClientSock.close()
sock.close()
复制代码
Client:

相关文章
|
2月前
|
机器学习/深度学习 PyTorch TensorFlow
|
2月前
|
机器学习/深度学习 搜索推荐 TensorFlow
Python 深度学习架构实用指南:第一、二部分(3)
Python 深度学习架构实用指南:第一、二部分(3)
60 2
|
2月前
|
机器学习/深度学习 并行计算 算法框架/工具
在Python中进行深度学习环境准备
在Python中进行深度学习环境准备
59 4
|
8天前
|
机器学习/深度学习 网络协议 Python
Python Socket深度学习分享
Python Socket深度学习分享
|
2月前
|
机器学习/深度学习 数据采集 TensorFlow
在Python中进行深度学习处理
在Python中进行深度学习处理
51 2
|
2月前
|
机器学习/深度学习 算法 算法框架/工具
Python 深度学习(一)(3)
Python 深度学习(一)
39 2
|
2月前
|
机器学习/深度学习 存储 TensorFlow
Python 深度学习(一)(4)
Python 深度学习(一)
34 2
|
2月前
|
机器学习/深度学习 人工智能 算法
Python 深度学习(一)(1)
Python 深度学习(一)
23 1
|
2月前
|
机器学习/深度学习 算法 自动驾驶
Python 深度学习(一)(2)
Python 深度学习(一)
34 1
|
2月前
|
机器学习/深度学习 运维 算法
Python 深度学习(三)(3)
Python 深度学习(三)
26 0
Python 深度学习(三)(3)

热门文章

最新文章

相关实验场景

更多