python3.x 基础八:socket网络编程
Socket
socket就是一直以来说的“套接字”,用于描述:ip:端口,是通信链的句柄,客户端通过这个句柄进行请求和响应
- 普通文件的操作顺序:打开-读写-关闭,针对的是文件
- socket是特殊的文件,操作顺序也是:打开-请求/相应-关闭,针对的是Client和Server之间的socket
整过过程如下:

socket方法
| bind(...)
| bind(address)
|
| Bind the socket to a local address. For IP sockets, the address is a
| pair (host, port); the host must refer to the local host. For raw packet
| sockets the address is a tuple (ifname, proto [,pkttype [,hatype]])
- 将套接字绑定大地址,如果是raw包,地址是元组形式(ip,port)
| close(...)
| close()
|
| Close the socket. It cannot be used after this call.
- 关闭套接字
| connect(...)
| connect(address)
|
| Connect the socket to a remote address. For IP sockets, the address
| is a pair (host, port).
- 链接到远程地址,如果是IP socket,地址是元组形式(ip,port)
| connect_ex(...)
| connect_ex(address) -> errno
|
| This is like connect(address), but returns an error code (the errno value)
| instead of raising an exception when an error occurs.
- 同connect,当发生错误时返回错误代码,而不是异常
| detach(...)
| detach()
|
| Close the socket object without closing the underlying file descriptor.
| The object cannot be used after this call, but the file descriptor
| can be reused for other purposes. The file descriptor is returned.
- 关闭套接字对象,而不关闭文件描述符,文件描述符类似文件句柄
| fileno(...)
| fileno() -> integer
|
| Return the integer file descriptor of the socket.
- 返回文件描述符
| getpeername(...)
| getpeername() -> address info
| Return the address of the remote endpoint. For IP sockets, the address
| info is a pair (hostaddr, port).
- 返回套接字的远程地址,一般是元组形式(ip,port)
| getsockname(...)
| getsockname() -> address info
|
| Return the address of the local endpoint. For IP sockets, the address
| info is a pair (hostaddr, port).
- 返回套接字自己的地址,一般是元组形式(ip,port)
| getsockopt(...)
| getsockopt(level, option[, buffersize]) -> value
|
| Get a socket option. See the Unix manual for level and option.
| If a nonzero buffersize argument is given, the return value is a
| string of that length; otherwise it is an integer.
- 获取套接字的选项
| gettimeout(...)
| gettimeout() -> timeout
|
| Returns the timeout in seconds (float) associated with socket
| operations. A timeout of None indicates that timeouts on socket
| operations are disabled.
- 以秒返回套接字操作超时时间
| listen(...)
| listen([backlog])
|
| Enable a server to accept connections. If backlog is specified, it must be
| at least 0 (if it is lower, it is set to 0); it specifies the number of
| unaccepted connections that the system will allow before refusing new
| connections. If not specified, a default reasonable value is chosen.
- 使server端开始监听链接,backlog至少未0,指定了尚未被accept处理的连接个数,如果达到这个数目,后面的连接将被拒绝,如果不指定,系统会设置一个合理值
| recv(...)
| recv(buffersize[, flags]) -> data
|
| Receive up to buffersize bytes from the socket. For the optional flags
| argument, see the Unix manual. When no data is available, block until
| at least one byte is available or until the remote end is closed. When
| the remote end is closed and all data is read, return the empty string.
- 从套接字中能接收到的数据,buffersize指定最多可以接收的大小,以bytes形式返回
| recv_into(...)
| recv_into(buffer, [nbytes[, flags]]) -> nbytes_read
|
| A version of recv() that stores its data into a buffer rather than creating
| a new string. Receive up to buffersize bytes from the socket. If buffersize
| is not specified (or 0), receive up to the size available in the given buffer.
|
| See recv() for documentation about the flags.
- 与recv类似,保存数据到缓存,而不是创建接收字符串
| recvfrom(...)
| recvfrom(buffersize[, flags]) -> (data, address info)
|
| Like recv(buffersize, flags) but also return the sender's address info.
- 与recv类似,返回值是元组形式(data,address),data是包含接收数据的字符串,address是发送数据的套接字地址
| recvfrom_into(...)
| recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)
|
| Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info.
- 与recv_into类似,返回的是发送者的地址信息
| recvmsg(...)
| recvmsg(bufsize[, ancbufsize[, flags]]) -> (data, ancdata, msg_flags, address)
| recvmsg_into(...)
| recvmsg_into(buffers[, ancbufsize[, flags]]) -> (nbytes, ancdata, msg_flags, address)
| sendall(...)
| sendall(data[, flags])
|
| Send a data string to the socket. For the optional flags
| argument, see the Unix manual. This calls send() repeatedly
| until all data is sent. If an error occurs, it's impossible
| to tell how much data has been sent.
- 循环发送,直到发送完毕
| sendmsg(...)
| sendmsg(buffers[, ancdata[, flags[, address]]]) -> count
|
| Send normal and ancillary data to the socket, gathering the
| non-ancillary data from a series of buffers and concatenating it into
| a single message. The buffers argument specifies the non-ancillary
| data as an iterable of bytes-like objects (e.g. bytes objects).
| The ancdata argument specifies the ancillary data (control messages)
| as an iterable of zero or more tuples (cmsg_level, cmsg_type,
| cmsg_data), where cmsg_level and cmsg_type are integers specifying the
| protocol level and protocol-specific type respectively, and cmsg_data
| is a bytes-like object holding the associated data. The flags
| argument defaults to 0 and has the same meaning as for send(). If
| address is supplied and not None, it sets a destination address for
| the message. The return value is the number of bytes of non-ancillary
| data sent.
|
| sendto(...)
| sendto(data[, flags], address) -> count
|
| Like send(data, flags) but allows specifying the destination address.
| For IP sockets, the address is a pair (hostaddr, port).
- 类似send,将数据发送到套接字,指定远端目标地址,返回的是发送字节数,多用于UDP协议
| setblocking(...)
| setblocking(flag)
|
| Set the socket to blocking (flag is true) or non-blocking (false).
| setblocking(True) is equivalent to settimeout(None);
| setblocking(False) is equivalent to settimeout(0.0).
- 是否阻塞,默认是True,等同settimeout(None),如果是False,accept和recv时无数据则报错,等同于settimeout(0.0)
| setsockopt(...)
| setsockopt(level, option, value)
|
| Set a socket option. See the Unix manual for level and option.
| The value argument can either be an integer or a string.
- 设置套接字选项,值参数可以是整形或者字符串
| settimeout(...)
| settimeout(timeout)
|
| Set a timeout on socket operations. 'timeout' can be a float,
| giving in seconds, or None. Setting a timeout of None disables
| the timeout feature and is equivalent to setblocking(1).
| Setting a timeout of zero is the same as setblocking(0).
- 设置套接字操作的超时时间,timeout是一个浮点数秒,None表示没有超时,指定超时时间等同setblocking(1),就是阻塞,如果没有超时间等同setblock(0)
| shutdown(...)
| shutdown(flag)
|
| Shut down the reading side of the socket (flag == SHUT_RD), the writing side
| of the socket (flag == SHUT_WR), or both ends (flag == SHUT_RDWR).
- 关闭socket读的一端或者写的一端,或者同时关闭两端
client:
import socket
client=socket.socket() # 实例化一个socket类,同时生成socket链接对象
client.connect(('localhost',8080)) # 链接目标,元组类型的主机+端口
while True:
msg=input('pls input something>>').strip()
if not bool(msg):
print('未输入任何内容')
continue
client.send(msg.encode("utf-8")) # 指定编码格式utf-8 ,发送数据,python3只能发送byte类型
data=client.recv(1024) # 接受窗口缓存大小,如果接收的内容大于窗口,则服务器的发送缓存会一直发送过来
print(data.decode()) # 将byte类型转换成unicode
client.close()
1.发送字符串
接收客户端请求,并返回结果的server:
import socket
server=socket.socket()
server.bind(('localhost',8080)) # 服务端绑定监听主机+端口
server.listen() #监听
while True:
print('begin to listening...')
conn,addr=server.accept()
#server.accept 等待链接进来,如果没有链接进来,则一直阻塞在这里
#不能用data=server。accept()直接接收,多个链接进来会无法区分是哪个链接
#使用标记方法,accept会返回两个值,一个是标记位,就是socket的句柄,一个是链接地址
print('conn:%s \n adr:%s'%(conn,addr))
print('conntion is coming')
while True:
data=conn.recv(1024) # 设置socket句柄接收的窗口大小
print('recv:',data.decode('utf-8'))
if not data:
print('client is lost')
break
conn.send(data.upper()) # 发送请求结果给客户端
server.close()
2.发送linux命令
返回命令结果给客户端
import os
import socket
server=socket.socket()
server.bind(('localhost',8020))
server.listen()
while True:
print('begin to listening...')
conn, addr = server.accept()
# print('conn:%s \n adr:%s' % (conn, addr))
print('conntion is coming')
while True:
data = conn.recv(1024)
print('I had recv your request %s'%data)
data=str(data,encoding="utf-8")
print('data is %s'%data)
print('change byte to str',data)
if not data:
print('client is lost')
break
res=os.popen(data).read()
conn.send(bytes(res,encoding="utf-8"))
print(len(data))
server.close()
3.传送文件
client
import socket
client=socket.socket() # 实例化一个socket类,同时生成socket链接对象
client.connect(('localhost',8010)) # 链接目标,元组类型的主机+端口
fd=open('../file.zip','wb')
while True:
msg=input('pls input something>>').strip()
if not bool(msg):
print('未输入任何内容')
continue
client.send(msg.encode("utf-8")) # 指定编码格式utf-8 ,发送数据,python3只能发送byte类型
# data=client.recv(1024) # 接受窗口缓存大小,如果接收的内容大于窗口,则服务器的发送缓存会一直发送过来
file=client.recv(10240000)
print('recv size is ',len(file))
fd.write(file)
fd.flush()
client.close()
server
import socket
server=socket.socket()
server.bind(('localhost',8010))
server.listen()
while True:
print('begin to listening...')
conn, addr = server.accept()
print('conn:%s \n adr:%s' % (conn, addr))
print('conntion is coming')
while True:
data = conn.recv(1024)
print('I had recv your request %s'%data)
data=str(data,encoding="utf-8")
print('change byte to str',data)
if not data:
print('client is lost')
break
with open('../MDMCSL71008_40-20004526.ZIP','rb') as fdr:
file=fdr.read()
print('file size is ',len(file))
conn.sendall(file)
print('file had ben sent')
break server.close()
注意:接收和发送的缓存有上限,大文件不能一次发送过来或者一次接收
python3.x 基础八:socket网络编程的更多相关文章
- Python之路【第七篇】python基础 之socket网络编程
本篇文章大部分借鉴 http://www.cnblogs.com/nulige/p/6235531.html python socket 网络编程 一.服务端和客户端 BS架构 (腾讯通软件:ser ...
- Python基础-week07 Socket网络编程
一 客户端/服务器架构 1.定义 又称为C/S架构,S 指的是Server(服务端软件),C指的是Client(客户端软件) 本章的中点就是教大写写一个c/s架构的软件,实现服务端软件和客户端软件基于 ...
- Java基础篇Socket网络编程中的应用实例
说到java网络通讯章节的内容,刚入门的学员可能会感到比较头疼,应为Socket通信中一定会伴随有IO流的操作,当然对IO流比较熟练的哥们会觉得这是比较好玩的一章,因为一切都在他们的掌握之中,这样操作 ...
- python基础(15)-socket网络编程&socketserver
socket 参数及方法说明 初始化参数 sk = socket.socket(参数1,参数2,参数3) 参数1:地址簇 socket.AF_INET IPv4(默认) socket.AF_INET6 ...
- Java : java基础(5) Socket网络编程
使用 DatagramSocket 创建一个 UDP协议的Socket, 用DatagramPacket创建一个数据包,可以指定ip和端口号以及包的数据,用socket.send()可以发送这个数据包 ...
- Java Web 基础(一) 基于TCP的Socket网络编程
一.Socket简单介绍 Socket通信作为Java网络通讯的基础内容,集中了异常.I/O流模式等众多知识点.学习Socket通信,既能够了解真正的网络通讯原理,也能够增强对I/O流模式的理解. 1 ...
- Socket网络编程-基础篇
Socket网络编程 网络通讯三要素: IP地址[主机名] 网络中设备的标识 本地回环地址:127.0.0.1 主机名:localhost 端口号 用于标识进程的逻辑地址 有效端口:0~65535 其 ...
- 【linux高级程序设计】(第十三章)Linux Socket网络编程基础 2
BSD Socket网络编程API 创建socket对象 int socket (int __domain, int __type, int __protocol) :成功返回socket文件描述符, ...
- python第八周:socket网络编程
1.socket网络编程 1.1概念: 网络套接字是跨计算机网络的连接的端点.今天,计算机之间的大多数通信都基于互联网协议;因此大多数网络套接字都是Internet套接字.更准确地说,套接字是一个句柄 ...
- Python面向对象进阶和socket网络编程-day08
写在前面 上课第八天,打卡: 为什么坚持?想一想当初: 一.面向对象进阶 - 1.反射补充 - 通过字符串去操作一个对象的属性,称之为反射: - 示例1: class Chinese: def __i ...
随机推荐
- 2019-2020-1 20199326《Linux内核原理与分析》第四周作业
第四周学习内容 庖丁解牛Linux内核分析第三章:MenuOS的构造 Linux内核分析实验三 学到的一些知识 操作系统两大宝剑:1.中断上下文的切换--保存现场和恢复现场 2.进程上下文的切换 Li ...
- ansible的模块使用
转载于 https://www.cnblogs.com/franknihao/p/8631302.html [Ansible 模块] 就如python库一样,ansible的模块也分成了基本模块和 ...
- weblogic创建域
一.webLogic服务域创建 https://blog.csdn.net/github_38922197/article/details/75097320
- 曹工力荐:调试 jdk 中 rt.jar 包部分的源码(可自由增加注释,修改代码并debug)
背景 大家知道,jdk安装的目录下,一般会有个src.zip包,这个包基本对应了rt.jar这个包.rt.jar这个包里面,就放了jdk中,jdk采用java实现的那部分类库代码,比如java.lan ...
- 标准库 xml
xml处理模块 xml是实现不同语言或程序之间进行数据交换的协议,跟json差不多,但json使用起来更简单,不过,古时候,在json还没诞生的黑暗年代,大家只能选择用xml呀,至今很多传统公司如金融 ...
- 《Splunk智能运维实战》——1.7 为本书加载样本数据
本节书摘来自华章计算机<Splunk智能运维实战>一书中的第1章,第1.7节,作者 [美]乔史·戴昆(Josh Diakun),保罗R.约翰逊(Paul R. Johnson),德莱克·默 ...
- Leetcode---Solutions&Notes
Leetcode已经成为面试必备技能之一,为了紧随潮流,也模仿大佬们刷刷题. 1.采用"龟系"做法,每道题尽量做到时间复杂度和空间复杂度的较优水平: 2.每道题的Solution先 ...
- 用Eclipse开发项目,你不能不知道的快捷键
1. 编辑快捷键 编辑快捷键 介绍 psvm + Tab 生成main方法 sout + tab 生成输出语句 Ctrl+X / Ctrl + Y 删除一行 Ctrl+D 复制一行 Ctrl+/ 或 ...
- redis系列之5----redis实战(redis与spring整合,分布式锁实现)
本文是redis学习系列的第五篇,点击下面链接可回看系列文章 <redis简介以及linux上的安装> <详细讲解redis数据结构(内存模型)以及常用命令> <redi ...
- 支付宝小程序云开发(Serverless)
支付宝小程序云开发(Serverless) 博客说明 文章所涉及的资料来自互联网整理和个人总结,意在于个人学习和经验汇总,如有什么地方侵权,请联系本人删除,谢谢! 一.在支付宝账号里面开通小程序云服务 ...