首页 > 编程语言 >python tcp socket 源码分享

python tcp socket 源码分享

时间:2023-02-01 15:34:41浏览次数:52  
标签:client socket python tcp server 源码 TCP data

服务端的源码:

import socketserver

class Handler_TCPServer(socketserver.BaseRequestHandler):
    """
    The TCP Server class for demonstration.

    Note: We need to implement the Handle method to exchange data
    with TCP client.

    """

    def handle(self):
        # self.request - TCP socket connected to the client
        self.data = self.request.recv(1024).strip()
        print("{} sent:".format(self.client_address[0]))
        print(self.data)
        # just send back ACK for data arrival confirmation
        self.request.sendall("ACK from TCP Server".encode())

if __name__ == "__main__":
    HOST, PORT = "localhost", 9999

    # Init the TCP server object, bind it to the localhost on 9999 port
    tcp_server = socketserver.TCPServer((HOST, PORT), Handler_TCPServer)

    # Activate the TCP server.
    # To abort the TCP server, press Ctrl-C.
    tcp_server.serve_forever()

客户端的源码:

import socket

host_ip, server_port = "127.0.0.1", 9999
data = " Hello how are you?\n"

# Initialize a TCP client socket using SOCK_STREAM
tcp_client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

try:
    # Establish connection to TCP server and exchange data
    tcp_client.connect((host_ip, server_port))
    tcp_client.sendall(data.encode())

    # Read data from the TCP server and close the connection
    received = tcp_client.recv(1024)
finally:
    tcp_client.close()

print ("Bytes Sent:     {}".format(data))
print ("Bytes Received: {}".format(received.decode()))

 

标签:client,socket,python,tcp,server,源码,TCP,data
From: https://www.cnblogs.com/dylancao/p/17082950.html

相关文章

  • Python服务进程管理工具supervisor使用记录
    [本文出自天外归云的博客园]学习资料supervisor文档:http://supervisord.org/running.html踩坑总结问题1:提示找不到一些包含/tmp的路径需要修改supervisord.conf配置文......
  • Python操作MongoDB
    虽然经常使用mongodb,但是很多方法都是现用现查,难得有时间,简单整理一下:一、连接mongodb安装第三方库:pipinstallpymongo连接到mongodb服务器:importpymongo#ho......
  • 简谈源码-Picasso(v2.71828)
    概述:设计模式用到单例和建造者;网络请求使用OkHttp3;缓存算法使用LRU;线程切换使用Handler​​Picasso官网​​Picasso.get().load(url).into(iv);Picasso的常见使用步骤很简单......
  • python打包exe
    1、安装[​​pipinstallpyinstaller​​]2、打包成exe文件[​​pyinstallerxxx.py​​]3、测试打包好的exe文件4、打包方式例:​​pyinstaller-Fxxx.py​​//生成单......
  • 使用java python 实现 QI-页面排序-骑马钉
    链接:http://www.cnprint.org/bbs/thread/77/339531/......
  • 直播系统app源码,简洁好看的登录页面
    直播系统app源码,简洁好看的登录页面1.html <!DOCTYPEhtml><html><head>  <metacharset="UTF-8">  <title>登录界面</title>  <linkrel="stylesheet"hr......
  • 五彩斑斓的 Black —— Python 代码格式化工具
      https://muzing.top/posts/a29e4743/#  良好的Python代码应有良好的格式规范(不止于遵守 PEP8 ),使用一个更强大更专业的代码格式化工具,来替代编辑器自带的......
  • Python 中global 关键字理解
    Python中的global关键字,你了解吗?前言今天来了解下Python中的global关键字。Python变量的作用域实战案例演示之前,先要了解下Python的作用域.Python变量的作......
  • python 中给文件加锁——fcntl模块
    如果没有fcntl模块则用sudopipinstallfcntl安装模块简单说明:打开文件,不存在则创建之f=open('./test','w')fcntl.flock(f,fcntl.LOCK_EX)这样就对文件t......
  • python实现区块链代码
    如果你明白了原理其实挺简单的。加密算法是python自带的需要导入hashlibimporthashlibashashsha=hasher.sha256()sha.update('yourcontent')printsha.hexdigest()输......