python socket recvfrom速度不够_浅谈python socket编程

本节内容:1、socket2、IO多路复用3、socketserversocketsocket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用【打开】【读写】【关闭】模式来操作。socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭)基本上,Socket 是任何一种计算机网络通讯中最基础的内容...
摘要由CSDN通过智能技术生成

本节内容:

1、socket

2、IO多路复用

3、socketserver

socket

socket起源于Unix,而Unix/Linux基本哲学之一就是“一切皆文件”,对于文件用【打开】【读写】【关闭】模式来操作。socket就是该模式的一个实现,socket即是一种特殊的文件,一些socket函数就是对其进行的操作(读/写IO、打开、关闭)

基本上,Socket 是任何一种计算机网络通讯中最基础的内容。例如当你在浏览器地址栏中输入 http://www.cnblogs.com/ 时,你会打开一个套接字,然后连接到 http://www.cnblogs.com/ 并读取响应的页面然后然后显示出来。而其他一些聊天客户端如 gtalk 和 skype 也是类似。任何网络通讯都是通过 Socket 来完成的。

socket和file的区别:

1、file模块是针对某个指定文件进行【打开】【读写】【关闭】

2、socket模块是针对 服务器端 和 客户端Socket 进行【打开】【读写】【关闭】

那我们就先来创建一个socket服务端吧

importsocket

sk=socket.socket()

sk.bind(("127.0.0.1",8080))

sk.listen(5)

conn,address=sk.accept()

sk.sendall(bytes("Hello world",encoding="utf-8"))

server

importsocket

obj=socket.socket()

obj.connect(("127.0.0.1",8080))

ret= str(obj.recv(1024),encoding="utf-8")print(ret)

View Code

socket更多功能

def bind(self, address): #real signature unknown; restored from __doc__

"""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]])"""

'''将套接字绑定到本地地址。是一个IP套接字的地址对(主机、端口),主机必须参考本地主机。'''

pass

def close(self): #real signature unknown; restored from __doc__

"""close()

Close the socket. It cannot be used after this call."""

'''关闭socket'''

pass

def connect(self, address): #real signature unknown; restored from __doc__

"""connect(address)

Connect the socket to a remote address. For IP sockets, the address

is a pair (host, port)."""

'''将套接字连接到远程地址。IP套接字的地址'''

pass

def connect_ex(self, address): #real signature unknown; restored from __doc__

"""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."""

pass

def detach(self): #real signature unknown; restored from __doc__

"""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."""

'''关闭套接字对象没有关闭底层的文件描述符。'''

pass

def fileno(self): #real signature unknown; restored from __doc__

"""fileno() -> integer

Return the integer file descriptor of the socket."""

'''返回整数的套接字的文件描述符。'''

return0def getpeername(self): #real signature unknown; restored from __doc__

"""getpeername() -> address info

Return the address of the remote endpoint. For IP sockets, the address

info is a pair (hostaddr, port)."""

'''返回远程端点的地址。IP套接字的地址'''

pass

def getsockname(self): #real signature unknown; restored from __doc__

"""getsockname() -> address info

Return the address of the local endpoint. For IP sockets, the address

info is a pair (hostaddr, port)."""

'''返回远程端点的地址。IP套接字的地址'''

pass

def getsockopt(self, level, option, buffersize=None): #real signature unknown; restored from __doc__

"""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."""

'''得到一个套接字选项'''

pass

def gettimeout(self): #real signature unknown; restored from __doc__

"""gettimeout() -> timeout

Returns the timeout in seconds (float) associated with socket

operations. A timeout of None indicates that timeouts on socket

operations are disabled."""

'''返回的超时秒数(浮动)与套接字相关联'''

returntimeoutdef ioctl(self, cmd, option): #real signature unknown; restored from __doc__

"""ioctl(cmd, option) -> long

Control the socket with WSAIoctl syscall. Currently supported 'cmd' values are

SIO_RCVALL: 'option' must be one of the socket.RCVALL_* constants.

SIO_KEEPALIVE_VALS: 'option' is a tuple of (onoff, timeout, interval)."""

return0def listen(self, backlog=None): #real signature unknown; restored from __doc__

"""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."""

'''使服务器能够接受连接。'''

pass

def recv(self, buffersize, flags=None): #real signature unknown; restored from __doc__

"""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."""

'''当没有数据可用,阻塞,直到至少一个字节是可用的或远程结束之前关闭。'''

pass

def recvfrom(self, buffersize, flags=None): #real signature unknown; restored from __doc__

"""recvfrom(buffersize[, flags]) -> (data, address info)

Like recv(buffersize, flags) but also return the sender's address info."""

pass

def recvfrom_into(self, buffer, nbytes=None, flags=None): #real signature unknown; restored from __doc__

"""recvfrom_into(buffer[, nbytes[, flags]]) -> (nbytes, address info)

Like recv_into(buffer[, nbytes[, flags]]) but also return the sender's address info."""

pass

def recv_into(self, buffer, nbytes=None, flags=None): #real signature unknown; restored from __doc__

"""recv_into(buffe

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值