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网络编程的更多相关文章

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

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

  2. Python基础-week07 Socket网络编程

    一 客户端/服务器架构 1.定义 又称为C/S架构,S 指的是Server(服务端软件),C指的是Client(客户端软件) 本章的中点就是教大写写一个c/s架构的软件,实现服务端软件和客户端软件基于 ...

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

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

  4. python基础(15)-socket网络编程&socketserver

    socket 参数及方法说明 初始化参数 sk = socket.socket(参数1,参数2,参数3) 参数1:地址簇 socket.AF_INET IPv4(默认) socket.AF_INET6 ...

  5. Java : java基础(5) Socket网络编程

    使用 DatagramSocket 创建一个 UDP协议的Socket, 用DatagramPacket创建一个数据包,可以指定ip和端口号以及包的数据,用socket.send()可以发送这个数据包 ...

  6. Java Web 基础(一) 基于TCP的Socket网络编程

    一.Socket简单介绍 Socket通信作为Java网络通讯的基础内容,集中了异常.I/O流模式等众多知识点.学习Socket通信,既能够了解真正的网络通讯原理,也能够增强对I/O流模式的理解. 1 ...

  7. Socket网络编程-基础篇

    Socket网络编程 网络通讯三要素: IP地址[主机名] 网络中设备的标识 本地回环地址:127.0.0.1 主机名:localhost 端口号 用于标识进程的逻辑地址 有效端口:0~65535 其 ...

  8. 【linux高级程序设计】(第十三章)Linux Socket网络编程基础 2

    BSD Socket网络编程API 创建socket对象 int socket (int __domain, int __type, int __protocol) :成功返回socket文件描述符, ...

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

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

  10. Python面向对象进阶和socket网络编程-day08

    写在前面 上课第八天,打卡: 为什么坚持?想一想当初: 一.面向对象进阶 - 1.反射补充 - 通过字符串去操作一个对象的属性,称之为反射: - 示例1: class Chinese: def __i ...

随机推荐

  1. HDU 5416 CBR and tree

    #include<bits/stdc++.h> using namespace std; #define for(i,a,b) for(int i=a;i<=b;++i) //T,N ...

  2. 【杂谈】从实现角度看ChannelFuture

    JDK中的Future特性 在介绍Netty的ChannelFuture之前,我们先来看看JDK中的Future是如何实现的.总的来说就是任务提交的时候会使用装饰器模式,将任务包装成一个FutureT ...

  3. 【DNS域名解析命令】host

    host - DNS lookup utility host命令是常用的分析域名查询工具,可以用来测试域名系统工作是否正常. 语法: host [-aCdlnrsTwv] [-c class] [-N ...

  4. Bubble Cup 11 - Finals [Online Mirror, Div. 1]题解 【待补】

    Bubble Cup 11 - Finals [Online Mirror, Div. 1] 一场很好玩的题啊! I. Palindrome Pairs 枚举哪种字符出现奇数次. G. AI robo ...

  5. CCF系列奖获奖名单公布,鲍虎军、周志华获CCF王选奖 | CNCC 2017

    本文讲的是CCF系列奖获奖名单公布,鲍虎军.周志华获CCF王选奖 | CNCC 2017, 由中国计算机学会(CCF)主办,福州市人民政府.福州大学承办,福建师范大学.福建工程学院协办的2017中国计 ...

  6. 【COCOS2DX-LUA 脚本开发之四】使用tolua++编译pk创建自定义类

    此篇基本[COCOS2DX(2.X)_LUA开发之三]在LUA中使用自定义精灵(LUA脚本与自创建类之间的访问)及LUA基础讲解 在Lua第三篇中介绍了,如何在cocos2dx中使用Lua创建自定义类 ...

  7. 第十届山东省赛L题Median(floyd传递闭包)+ poj1975 (昨晚的课程总结错了,什么就出度出度,那应该是叫讨论一个元素与其余的关系)

    Median Time Limit: 1 Second Memory Limit: 65536 KB Recall the definition of the median of elements w ...

  8. keras-深度学习处理文本数据

    深度学习用于自然语言处理是将模式识别应用于单词.句子和段落,这与计算机视觉是将模式识别应用于像素大致相同.深度学习模型不会接收原始文本作为输入,它只能处理数值张量,因此我们必须将文本向量化(vecto ...

  9. SpringBoot返回JSON日期格式问题

    SpringBoot中默认返回的日期格式类似于这样: "birth": 1537407384500 或者是这样: "createTime": "201 ...

  10. D. Kefa and Dishes(状压)

    永久打开的传送门 \(这次总算没有写砸........\) \(设f[i][j]为上一次吃的i物品状态为j的最大收益\) \(那么我们就暴力枚举所有状态i,然后在当前状态找出一个没吃的食物j,再去找一 ...