服务端:

  1. import socket
  2. server = socket.socket()
  3. #绑定要监听的端口
  4. server.bind(('localhost',6969))
  5. #监听
  6. server.listen()
  7. print("等待连接...")
  8. #等待连接,conn就是客户端连过来在服务端为其生成的一个连接实例
  9. conn,addr = server.accept()
  10. print("连接进行中...")
  11. print(conn,addr)
  12.  
  13. data = conn.recv(1024)
  14. print("recv:",data)
  15. conn.send(data.upper())
  16. server.close()

客户端:

  1. import socket
  2.  
  3. #声明socket类型,同时生产socket连接对象
  4. client = socket.socket()
  5. client.connect(('localhost',6969))
  6. client.send(b"hello world!")
  7. data = client.recv(1024)
  8. print("recv:",data)
  9. client.close()

服务端输出:

  1. 等待连接...
  2. 连接进行中...
  3. <socket.socket fd=328, family=AddressFamily.AF_INET, type=SocketKind.SOCK_STREAM, proto=0, laddr=('127.0.0.1', 6969), raddr=('127.0.0.1', 50248)> ('127.0.0.1', 50248)
  4. recv: b'hello world!'

客户端输出:

  1. recv: b'HELLO WORLD!'

连续发送信息:

服务端:

  1. import socket
  2. client = socket.socket()
  3. client.connect(('localhost',6969))
  4. while True:
  5.     msg = input(">>:").strip()
  6.     client.send(msg.encode("utf-8"))
  7.     data = client.recv(1024)
  8.     print("recv:",data.decode())
  9. client.close()

客户端:

  1. import socket
  2.  
  3. #声明socket类型,同时生产socket连接对象
  4. client = socket.socket()
  5. client.connect(('localhost',6969))
  6. client.send(b"hello world!")
  7. # client.send("我要下载".encode("utf-8"))
  8. data = client.recv(1024)
  9. print("recv:",data)
  10. client.close()

在linux上客户端执行命令

服务端:

  1. [root@test-c2c-console01 oldboy]# cat socket_server.py
  2. #-*-coding:utf-8-*-
  3. #_author_="golden"
  4. #服务端
  5. import socket
  6. import os
  7.  
  8. server = socket.socket()
  9. #绑定要监听的端口
  10. server.bind(('localhost',6969))
  11. #监听,最大连接数
  12. server.listen(5)
  13. print("等待连接...")
  14. while True:
  15.     #conn就是客户端连过来在服务端为其生成的一个连接实例
  16.     conn,addr = server.accept()
  17.     print("连接进行中...")
  18.     print(conn,addr)
  19.     while True:
  20.         data = conn.recv(1024)
  21.         print("recv:",data)
  22.         if not data:
  23.             print("客户端断开!")
  24.             break
  25.         res = os.popen(data).read()
  26.         conn.send(res)
  27. server.close()

客户端:

  1. [root@test-c2c-console01 oldboy]# cat socket_client.py
  2. #-*-coding:utf-8-*-
  3. _author_ = "golden"
  4. #客户端
  5. import socket
  6. #声明socket类型,同时生成socket连接对象
  7. client = socket.socket()
  8. client.connect(('localhost',6969))
  9. while True:
  10.     msg = raw_input(">>:").strip()
  11.     if len(msg) == 0:continue
  12.     client.send(msg.encode("utf-8"))
  13.     data = client.recv(102400)
  14.     print(data.decode())
  15.  
  16. client.close()

linux发送文件

服务端:

  1. [root@test-c2c-console01 oldboy]# cat socket_server.py
  2. #-*-coding:utf-8-*-
  3. #_author_="golden"
  4. #服务端
  5. import socket
  6. import os
  7.  
  8. server = socket.socket()
  9. #绑定要监听的端口
  10. server.bind(('localhost',6969))
  11. #监听,最大连接数
  12. server.listen(5)
  13. print("等待连接...")
  14. while True:
  15.     #conn就是客户端连过来在服务端为其生成的一个连接实例
  16.     conn,addr = server.accept()
  17.     print("连接进行中...")
  18.     print(conn,addr)
  19.     while True:
  20.         data = conn.recv(1024)
  21.         print("recv:",data)
  22.         if not data:
  23.             print("客户端断开!")
  24.             break
  25.         #res = os.popen(data).read()
  26.         #conn.send(res)
  27.         f = open("move.mkv")
  28.         data = f.read()
  29.         conn.send(data)
  30. server.close()

客户端:

  1. [root@test-c2c-console01 oldboy]# cat socket_client.py
  2. #-*-coding:utf-8-*-
  3. _author_ = "golden"
  4. #客户端
  5. import socket
  6. #声明socket类型,同时生成socket连接对象
  7. client = socket.socket()
  8. client.connect(('localhost',6969))
  9. while True:
  10.     msg = raw_input(">>:").strip()
  11.     if len(msg) == 0:continue
  12.     client.send(msg.encode("utf-8"))
  13.     data = client.recv(102400)
  14.     #print(data.decode())
  15.     f = open("video.avi",'wb')
  16.     f.write(data)
  17.     f.close()
  18.  
  19. client.close()

一次最多发送的数据量32768(不同系统有区别)

  1. [root@test-c2c-console01 oldboy]# ll *.avi *.mkv
  2. -rw-r--r-- 1 root root 681790584 Dec 27 19:26 move.mkv
  3. -rw-r--r-- 1 root root 32768 Feb 12 08:47 video.avi

多次发送

服务端:

  1. [root@test-c2c-console01 oldboy]# cat socket_server.py
  2. #-*-coding:utf-8-*-
  3. #_author_="golden"
  4. #服务端
  5. import socket
  6. import os
  7.  
  8. server = socket.socket() #实例化一个socket
  9. #绑定要监听的端口
  10. server.bind(('localhost',6969))
  11. #监听,最大连接数
  12. server.listen(5)
  13. print("等待连接...")
  14. while True:
  15.     #conn就是客户端连过来在服务端为其生成的一个连接实例
  16.     conn,addr = server.accept()
  17.     print("连接进行中...")
  18.     print(conn,addr)
  19.     while True:
  20.         data = conn.recv(1024)
  21.         print("recv:",data)
  22.         if not data:
  23.             print("客户端断开!")
  24.             break
  25.         #res = os.popen(data).read()
  26.         #conn.send(res)
  27.         f = open("move.mkv")
  28.         data = f.read()
  29.         print(len(data))
  30.         conn.send(data)
  31. server.close()

客户端:

  1. [root@test-c2c-console01 oldboy]# cat socket_client.py
  2. #-*-coding:utf-8-*-
  3. _author_ = "golden"
  4. #客户端
  5. import socket
  6. #声明socket类型,同时生成socket连接对象
  7. client = socket.socket()
  8. client.connect(('localhost',6969))
  9. f = open("video.avi",'wb')
  10. while True:
  11.     msg = raw_input(">>:").strip()
  12.     if len(msg) == 0:continue
  13.     client.send(msg.encode("utf-8"))
  14.     data = client.recv(102400)
  15.     #print(data.decode())
  16.     f.write(data)
  17.     f.flush()
  18.  
  19. client.close()

每交互一次文件的变化:

  1. [root@test-c2c-console01 oldboy]# ll
  2. total 665832
  3. -rw-r--r-- 1 root root 681790584 Dec 27 19:26 move.mkv
  4. -rw-r--r-- 1 root root 0 Feb 12 09:08 video.avi
  5. [root@test-c2c-console01 oldboy]# ll
  6. total 665864
  7. -rw-r--r-- 1 root root 681790584 Dec 27 19:26 move.mkv
  8. -rw-r--r-- 1 root root 32768 Feb 12 09:08 video.avi
  9. [root@test-c2c-console01 oldboy]# ll
  10. total 665964
  11. -rw-r--r-- 1 root root 681790584 Dec 27 19:26 move.mkv
  12. -rw-r--r-- 1 root root 135168 Feb 12 09:08 video.avi
  13. [root@test-c2c-console01 oldboy]# ll
  14. total 666164
  15. -rw-r--r-- 1 root root 681790584 Dec 27 19:26 move.mkv
  16. -rw-r--r-- 1 root root 339968 Feb 12 09:09 video.avi
  17. [root@test-c2c-console01 oldboy]# ll
  18. total 666164
  19. -rw-r--r-- 1 root root 681790584 Dec 27 19:26 move.mkv
  20. -rw-r--r-- 1 root root 339968 Feb 12 09:09 video.avi

socket实现简单的ssh

服务端:

  1. import socket,os
  2. server = socket.socket()
  3. server.bind(('localhost',9999))
  4. server.listen()
  5. while True:
  6.     conn,addr = server.accept()
  7.     print("new conn:",addr)
  8.     while True:
  9.         data = conn.recv(1024)
  10.         if not data:
  11.             print("客户端已断开!")
  12.             break
  13.         print("执行指令:",data)
  14.         cmd_res = os.popen(data.decode()).read() #接收字符串,执行结果也是字符串
  15.         print("before send ",len(cmd_res))
  16.         if len(cmd_res) ==0:
  17.             cmd_res = "cmd has no output"
  18.         conn.send(str(len(cmd_res.encode())).encode("utf-8"))#先发大小给客户端
  19.         conn.send(cmd_res.encode("utf-8"))
  20.  
  21. server.close()

客户端:

  1. import socket
  2. client = socket.socket()
  3. client.connect(('localhost',9999))
  4.  
  5. while True:
  6.     cmd = input(">>:").strip()
  7.     if len(cmd) == 0:continue
  8.     client.send(cmd.encode("utf-8"))
  9.     cmd_res_size = client.recv(1024)#接收大小
  10.     print("结果大小:",cmd_res_size)
  11.     received_size = 0
  12.     received_data = b''
  13.     while received_size < int(cmd_res_size.decode()):
  14.         data = client.recv(1024)
  15.         received_size += len(data) #每次收到的有可能小于1024,所以必须用len判断
  16.         # print(data.decode())
  17.         received_data += data
  18.     else:
  19.         print("cmd res receive done...",received_size)
  20.         print(received_data.decode())
  21.  
  22. client.close()

注意socket粘包问题。

方法一:在多个相连的send之间添加sleep。

  1. conn.send(str(len(cmd_res.encode())).encode("utf-8"))#先发大小给客户端
  2. time.sleep(0.5)
  3. conn.send(cmd_res.encode("utf-8"))

方法二:在每次send之后进行一次交互。

服务端:

  1. conn.send(str(len(cmd_res.encode())).encode("utf-8"))#先发大小给客户端client_ack = conn.recv(1024) #wait client to confirm
  2. print(client_ack.decode())
  3. conn.send(cmd_res.encode("utf-8"))

客户端:

  1. cmd_res_size = client.recv(1024)#接收大小
  2. client.send("已准备好接收数据!".encode("utf-8"))

socket实现简单的ftp

ftp server:

1、读取文件名

2、检测文件是否存在

3、打开文件

4、检测文件大小

5、发送文件大小给客户端

6、等待客户端确认

7、开始边读边发数据

8、发送md5

服务端:

  1. import socket,os,hashlib
  2. server = socket.socket()
  3. server.bind(("localhost",9999))
  4. server.listen()
  5. while True:
  6.     conn,addr = server.accept()
  7.     print("new conn:",addr)
  8.     while True:
  9.         print("等待指令...")
  10.         data = conn.recv(1024)
  11.         if not data:
  12.             print("客户端已断开!")
  13.             break
  14.         cmd,filename = data.decode().split()
  15.         print(filename)
  16.         if os.path.isfile(filename):
  17.             f = open(filename,"rb")
  18.             m = hashlib.md5()
  19.             file_size = os.stat(filename).st_size
  20.             conn.send(str(file_size).encode()) #send file size
  21.             conn.recv(1024) #wait for ack
  22.             for line in f:
  23.                 m.update(line)
  24.                 conn.send(line)
  25.             print("file md5",m.hexdigest())
  26.             f.close()
  27.             conn.send(m.hexdigest().encode()) #sed med5
  28.         print("send done")
  29.  
  30. server.close()

客户端:

  1. import socket,hashlib
  2. client = socket.socket()
  3. client.connect(('localhost',9999))
  4. while True:
  5.     cmd = input(">>:").strip()
  6.     if len(cmd) == 0:continue
  7.     if cmd.startswith("get"):
  8.         client.send(cmd.encode())
  9.         server_response = client.recv(1024)
  10.         print("server_response:",server_response)
  11.         client.send(b"read to recv file")
  12.         file_total_size = int(server_response.decode())
  13.         received_size = 0
  14.         filename = cmd.split()[1]
  15.         f = open(filename + ".new","wb")
  16.         m = hashlib.md5()
  17.         while received_size < file_total_size:
  18.             if file_total_size - received_size > 1024:
  19.                 size = 1024
  20.             else:
  21.                 size = file_total_size - received_size
  22.  
  23.             data = client.recv(size)
  24.             received_size += len(data)
  25.             m.update(data)
  26.             f.write(data)
  27.             # print(file_total_size,received_size)
  28.         else:
  29.             new_file_md5 = m.hexdigest()
  30.             print("file recv done",received_size,file_total_size)
  31.             f.close()
  32.         server_file_md5 = client.recv(1024)
  33.         print("server file md5:{0},client file md5:{1}".format(server_file_md5,new_file_md5))
  34. client.close()

SocketServer

实现并发处理。

The socketserver module simplifies the task of writing network servers.

socketserver常用类型

class socketserver.TCPServer(server_address,RequestHandlerClass,bind_and_activate=True)

This uses the Internet TCP protocol,which provides for continuous streams of data between the client and server.

class socketserver.UDPServer(server_address,RequestHandlerClass,bind_and_activate=True)

This uses datagrams,which are discrete packets of information that may arrive out of order or be lost while in transit.The parameters are the same as for TCPServer.

创建一个socketserver至少分一下几步:

1、You mast create a request handler class by subclassing the BaseRequestHandler class and overriding its handle() method;this method will process incoming requests.

创建一个请求处理类继承BaseRequestHandler,并且重写父类handle()。

2、You mast instantiate one of the server classes,passing it the server's address and the request handler class.

实例化TCPServer,并传递server ip和上述创建的请求处理类给这个TCPServer。

3、Then call the handle_request() or server_forever() method of the server object to process one or many requests.

server.handle_request():只处理一个请求

server.server_forever():处理多个请求

4、Call server_close() to close the socket.

服务端:

  1. import socketserver
  2.  
  3. class MyTCPHandler(socketserver.BaseRequestHandler):
  4.  
  5.     def handle(self):
  6.         while True:
  7.             try:
  8.                 self.data = self.request.recv(1024).strip()
  9.                 print("{} wrote :".format(self.client_address[0]))
  10.                 print(self.data)
  11.                 self.request.send(self.data.upper())
  12.             except ConnectionResetError as CR:
  13.                 print("err",CR)
  14.                 break
  15.  
  16. if __name__ == "__main__":
  17.     HOST,PORT = "localhost",9999
  18.     # server = socketserver.TCPServer((HOST,PORT),MyTCPHandler) # 单线程
  19.     server = socketserver.ThreadingTCPServer((HOST,PORT),MyTCPHandler) # 多线程,ForkingTCPServer多进程
  20.     server.serve_forever()

客户端:

  1. import socket
  2.  
  3. client = socket.socket()
  4. client.connect(("localhost",9999))
  5. while True:
  6.     msg = input(">>:").strip()
  7.     if len(msg) == 0:continue
  8.     client.send(msg.encode("utf-8"))
  9.     data = client.recv(1024)
  10.     print("recv:",data.decode())
  11. client.close()

开启一个新线程

  1. def process_request(self, request, client_address):
  2.     """Start a new thread to process the request."""
  3.     t = threading.Thread(target = self.process_request_thread,
  4.                          args = (request, client_address))
  5.     t.daemon = self.daemon_threads
  6.     t.start()

 

Python开发【第八篇】:socket网络编程的更多相关文章

  1. 【python自动化第八篇:网络编程】

    一.拾遗 动态导入模块 目的是为了在导入模块的过程中将模块以字符的格式导入. #!/usr/bin/env python # -*- coding:utf-8 -*- #Author:wanghui ...

  2. python面向对象、类、socket网络编程

    类和对象 python3统一了类与类型的概念:类==类型:从一组对象中提取相似的部分就是类:特征与技能的结合体就叫做对象: 类的功能: 初始实例化: 属性引用: 1.数据属性: 2.函数属性: 对于一 ...

  3. python学习之路-9 socket网络编程

    socket基础 socket通常也称作"套接字",用于描述IP地址和端口,是一个通信链的句柄,应用程序通常通过"套接字"向网络发出请求或者应答网络请求. so ...

  4. Java基础篇Socket网络编程中的应用实例

    说到java网络通讯章节的内容,刚入门的学员可能会感到比较头疼,应为Socket通信中一定会伴随有IO流的操作,当然对IO流比较熟练的哥们会觉得这是比较好玩的一章,因为一切都在他们的掌握之中,这样操作 ...

  5. python3.x 基础八:socket网络编程

    Socket socket就是一直以来说的“套接字”,用于描述:ip:端口,是通信链的句柄,客户端通过这个句柄进行请求和响应 普通文件的操作顺序:打开-读写-关闭,针对的是文件 socket是特殊的文 ...

  6. Python之路【第七篇】python基础 之socket网络编程

    本篇文章大部分借鉴 http://www.cnblogs.com/nulige/p/6235531.html python socket  网络编程 一.服务端和客户端 BS架构 (腾讯通软件:ser ...

  7. 从零开始学Python第八周:网络编程基础(socket)

    Socket网络编程 一,Socket编程 (1)Socket方法介绍 Socket是网络编程的一个抽象概念.通常我们用一个Socket表示"打开了一个网络链接",而打开一个Soc ...

  8. python第八周:socket网络编程

    1.socket网络编程 1.1概念: 网络套接字是跨计算机网络的连接的端点.今天,计算机之间的大多数通信都基于互联网协议;因此大多数网络套接字都是Internet套接字.更准确地说,套接字是一个句柄 ...

  9. Python全栈【Socket网络编程】

    Python全栈[socket网络编程] 本章内容: Socket 基于TCP的套接字 基于UDP的套接字 TCP粘包 SocketServer 模块(ThreadingTCPServer源码剖析) ...

  10. python之Socket网络编程

    什么是网络? 网络是由节点和连线构成,表示诸多对象及其相互联系.在数学上,网络是一种图,一般认为专指加权图.网络除了数学定义外,还有具体的物理含义,即网络是从某种相同类型的实际问题中抽象出来的模型.在 ...

随机推荐

  1. hello2 源码解析

    在hello2的项目中,采用的是Java servlet  技术来采取对项目的整体框架的搭建.编写另一个greeting的java文件,实现了一个greeting的java类来覆盖url的doGet方 ...

  2. 安卓控制LED驱动编写

    开发平台 * 芯灵思SinlinxA33开发板 淘宝店铺: https://sinlinx.taobao.com/ 嵌入式linux 开发板交流 QQ:641395230 打开Android Stud ...

  3. PythonStudy——Python 中Switch-Case 结构的实现

    学习Python过程中,发现Python没有Switch-case,过去写C习惯用Switch/Case语句,官方文档说通过if-elif实现.所以不妨自己来实现Switch-Case功能. 方法一 ...

  4. php 多线程

    windows下安装php真正的多线程扩展pthreads教程 http://www.thinkphp.cn/topic/22676.html PHP 安装 Pthreads (解决 class Th ...

  5. java_字段声明

    多字段继承,为避免混淆,simple name与qualified name的使用 package java20180129_1; interface Frob { float v=2.0f; } c ...

  6. 数字证书的理解以及自建CA机构颁发证书

    一.理解什么是数字证书   http://www.cnblogs.com/JeffreySun/archive/2010/06/24/1627247.html    理解数字证书等概念,无数次想好好看 ...

  7. spring cloud gateway - RequestRateLimiter

    1. Official website 5.7 RequestRateLimiter GatewayFilter Factory The RequestRateLimiter GatewayFilte ...

  8. SpringMVC的工作流程、组件说明以及常用注解说明

    1. SpringMVC处理流程 2. SpringMVC架构 2.1 框架结构 2.2 框架流程 1. 用户发送请求至前端控制器DispatcherServlet. 2. DispatcherSer ...

  9. dubbo文档

    Srping版Dubbo集成中文地址: https://dubbo.gitbooks.io/dubbo-user-book/content/preface/background.html Spring ...

  10. NodeJS client code websocket

    var WebSocketClient = require('websocket').client; var client = new WebSocketClient(); client.on('co ...