用python进行socket编程老是报错error: [Errno 10061] 怎么回事啊??好心人帮帮忙啊!!在线等!谢了

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了用python进行socket编程老是报错error: [Errno 10061] 怎么回事啊??好心人帮帮忙啊!!在线等!谢了相关的知识,希望对你有一定的参考价值。

# -*- coding: utf-8 -*-

import socket

RECEIVER_ADDRESS = '192.168.0.2'#RECEIVER的IP
RECEIVER_PORT = 8888#RECEIVER侦听的端口

class Sender():
def __init__(self):
#创建socket
try:
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
except socket.error , msg :
print "Failed to create socket."
return
print 'socket created'
self.makePDU()
self.send()
self.sock.close()
def makePDU(self): #本函数生成PDU
self.data='hello!'
print self.data
def send(self):
global data
#使用socket连接RECEIVER

global RECEIVER_ADDRESS
global RECEIVER_PORT
self.sock.connect((RECEIVER_ADDRESS,RECEIVER_PORT))
print 'Socket Connected to RECEIVER on ip ' + RECEIVER_ADDRESS
# 向RECEIVER发送PDU
try:
self.sock.sendall(data)
except socket.error:
print 'Send failed'
return
print 'Data send successfully'

if __name__ == "__main__":
Sender()

输出结果:
>>>
socket created
hello!

Traceback (most recent call last):
File "D:\study\python编程\1.1.py", line 41, in <module>
Sender()
File "D:\study\python编程\1.1.py", line 19, in __init__
self.send()
File "D:\study\python编程\1.1.py", line 30, in send
self.sock.connect((RECEIVER_ADDRESS,RECEIVER_PORT))
File "D:\Program Files\python2.7.5\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 10061]
>>>

出现Errno10061 是因为目标主机主动拒绝,连接不能建立。

这通常是因为试图连接到一个远程主机上不活动的服务,如没有服务器应用程序处于执行状态。

解决方法:

1)你先启动server,看是否可以正常启动。

2)因为是基于TCP的,所以你试试telnet可否链接到server上,格式为 telnet 127.0.0.1 5007 如果telnet没有找到的话,就配置下启动telnet。

3) 如果telnet没有成功的话,则看下是否允许python访问网络,在控制面板里配置下,如果不是要允许访问。

4)最后,服务端自己的监听地址和客户端书写的服务端地址形式必须要完全对应,或者服务端地址写成空字符串”“形式,表示所有可用地址。程序才能正常传输文件。

意思就是:

1. 服务端和客户端要在一台主机上做测试的话,要不全写成"localhost"或者全写成"127.0.0.1"形式,要不全写成自己的内网IP或外网IP,如"192.168.0.10"形式;
2. 不在同一个主机上的话,服务端本地地址不能写成"127.0.0.1"或者"localhost",而要写成自己的内网IP或外网IP形式。
3. 最简单一种方法:服务端地址写成""形式。

参考技术A RECEIVER_ADDRESS = '192.168.0.2'

这个ip能ping通不追问

我把引号里面的内容去掉之后还得上面那个结果了,我想知道逻辑有错误没呢??真的很感谢啊!!

找了一下午的错误了,我是初学者,才学2天,所以请多多关照啊!!

追答

10061 因为目标主机主动拒绝,连接不能建立。这通常是因为试图连接到一个远程主机上不活动的服务,如没有服务器应用程序处于执行状态

追问

我是前两天才开始接触这门语言的,所以完全是个菜鸟,望高手帮忙修改一下行不??只有能完成上面文字叙述的功能即可,定重金酬谢啊!!!

追答

换个端口试试

追问

这个端口没有问题,我检查过,是空闲的。主要是想实现在同一台机子上跑客户端和服务端。。。很棘手啊!!!

追答

防火墙关掉试试

追问

关了啊!!其他的程序都可以运行,连接到别的服务器都可以,就是没法实现本机连本机。。。高手啊!!!

追答

试下在self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)这句话下面添加:

self.sock.bind(('127.0.0.1', 8888))

追问

还是不行啊!!!!急死了!!!

追答

我可以啊,执行结果:
>>>
socket created
hello!
Socket Connected to RECEIVER on ip 127.0.0.1
Data send successfully
>>>
-----------------
就是在self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)下面加了这句:

self.sock.bind(('127.0.0.1', 8989))

然后最上面改为:
RECEIVER_ADDRESS = '127.0.0.1'#RECEIVER的IP
RECEIVER_PORT = 8989#RECEIVER侦听的端口

另外,send方法里面你声明global data,但是在你的代码没有全局data,所以会报错的,改为在send方法内部定义data或者在global定义data变量

追问

十分感谢亲啊!!!运行结果是没错了,不过我想问一下,这个是发送数据的一方,是客户端,客户端为什么也要绑定IP啊???还有我怎么用另外一个模块接收呢??

追答

嗯,你可以看下Python自带的手册,里面有例子和说明

追问

苦逼的我是前几天开始接触网络编程的,一切从零开始,又没有书,只有在网上搜电子书,而我又不习惯看电子书,并且能找到的例子都差不多了,那些例子都是把客户端和服务端分开的,我一会还要去赶火车,这个任务一会就必须交,所以相当郁闷的!!!

追答

嗯,那你就看自带的手册就行了,我看里面说的还是比较清楚的

本回答被提问者采纳
参考技术B 没发送完成就close了?

python之socket编程

python之socket编程

 

本章内容

  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 来完成的。

Python 官方关于 Socket 的函数请看 http://docs.python.org/library/socket.html

socket和file的区别:

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

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

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

import socket

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"))
import socket

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

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

 

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.
        """
        \'\'\'返回整数的套接字的文件描述符。\'\'\'
        return 0

    def 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.
        """
        \'\'\'返回的超时秒数(浮动)与套接字相关联\'\'\'
        return timeout

    def 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).
        """
        return 0

    def 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(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.
        """
        pass

    def send(self, data, flags=None): # real signature unknown; restored from __doc__
        """
        send(data[, flags]) -> count
        
        Send a data string to the socket.  For the optional flags
        argument, see the Unix manual.  Return the number of bytes
        sent; this may be less than len(data) if the network is busy.
        """
        \'\'\'发送一个数据字符串到套接字。\'\'\'
        pass

    def sendall(self, data, flags=None): # real signature unknown; restored from __doc__
        """
        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.
        """
        \'\'\'发送一个数据字符串到套接字,直到所有数据发送完成\'\'\'
        pass

    def sendto(self, data, flags=None, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 
        """
        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).
        """
        pass

    def setblocking(self, flag): # real signature unknown; restored from __doc__
        """
        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),如果设置False,那么accept和recv时一旦无数据,则报错。\'\'\'
        pass

    def setsockopt(self, level, option, value): # real signature unknown; restored from __doc__
        """
        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.
        """
        pass

    def settimeout(self, timeout): # real signature unknown; restored from __doc__
        """
        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).
        """
        pass

    def share(self, process_id): # real signature unknown; restored from __doc__
        """
        share(process_id) -> bytes
        
        Share the socket with another process.  The target process id
        must be provided and the resulting bytes object passed to the target
        process.  There the shared socket can be instantiated by calling
        socket.fromshare().
        """
        return b""

    def shutdown(self, flag): # real signature unknown; restored from __doc__
        """
        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).
        """
        pass

    def _accept(self): # real signature unknown; restored from __doc__
        """
        _accept() -> (integer, address info)
        
        Wait for an incoming connection.  Return a new socket file descriptor
        representing the connection, and the address of the client.
        For IP sockets, the address info is a pair (hostaddr, port).
        """
        pass
View Code
复制代码
    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.
        """
        \'\'\'返回整数的套接字的文件描述符。\'\'\'
        return 0

    def 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.
        """
        \'\'\'返回的超时秒数(浮动)与套接字相关联\'\'\'
        return timeout

    def 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).
        """
        return 0

    def 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(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.
        """
        pass

    def send(self, data, flags=None): # real signature unknown; restored from __doc__
        """
        send(data[, flags]) -> count
        
        Send a data string to the socket.  For the optional flags
        argument, see the Unix manual.  Return the number of bytes
        sent; this may be less than len(data) if the network is busy.
        """
        \'\'\'发送一个数据字符串到套接字。\'\'\'
        pass

    def sendall(self, data, flags=None): # real signature unknown; restored from __doc__
        """
        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.
        """
        \'\'\'发送一个数据字符串到套接字,直到所有数据发送完成\'\'\'
        pass

    def sendto(self, data, flags=None, *args, **kwargs): # real signature unknown; NOTE: unreliably restored from __doc__ 
        """
        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).
        """
        pass

    def setblocking(self, flag): # real signature unknown; restored from __doc__
        """
        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),如果设置False,那么accept和recv时一旦无数据,则报错。\'\'\'
        pass

    def setsockopt(self, level, option, value): # real signature unknown; restored from __doc__
        """
        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.
        """
        pass

    def settimeout(self, timeout): # real signature unknown; restored from __doc__
        """
        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).
        """
        pass

    def share(self, process_id): # real signature unknown; restored from __doc__
        """
        share(process_id) -> bytes
        
        Share the socket with another process.  The target process id
        must be provided and the resulting bytes object passed to the target
        process.  There the shared socket can be instantiated by calling
        socket.fromshare().
        """
        return b""

    def shutdown(self, flag): # real signature unknown; restored from __doc__
        """
        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).
        """
        pass

    def _accept(self): # real signature unknown; restored from __doc__
        """
        _accept() -> (integer, address info)
        
        Wait for an incoming connection.  Return a new socket file descriptor
        representing the connection, and the address of the client.
        For IP sockets, the address info is a pair (hostaddr, port).
        """
        pass
    
复制代码

注:撸主知道大家懒,所以把全部功能的中文标记在每个功能的下面啦。下面撸主列一些经常用到的吧

sk.bind(address)

  s.bind(address) 将套接字绑定到地址。address地址的格式取决于地址族。在AF_INET下,以元组(host,port)的形式表示地址。

sk.listen(backlog)

  开始监听传入连接。backlog指定在拒绝连接之前,可以挂起的最大连接数量。

      backlog等于5,表示内核已经接到了连接请求,但服务器还没有调用accept进行处理的连接个数最大为5
      这个值不能无限大,因为要在内核中维护连接队列

sk.setblocking(bool)

  是否阻塞(默认True),如果设置False,那么accept和recv时一旦无数据,则报错。

sk.accept()

  接受连接并返回(conn,address),其中conn是新的套接字对象,可以用来接收和发送数据。address是连接客户端的地址。

  接收TCP 客户的连接(阻塞式)等待连接的到来

sk.connect(address)

  连接到address处的套接字。一般,address的格式为元组(hostname,port),如果连接出错,返回socket.error错误。

sk.connect_ex(address)

  同上,只不过会有返回值,连接成功时返回 0 ,连接失败时候返回编码,例如:10061

sk.close()

  关闭套接字

sk.recv(bufsize[,flag])

  接受套接字的数据。数据以字符串形式返回,bufsize指定最多可以接收的数量。flag提供有关消息的其他信息,通常可以忽略。

sk.recvfrom(bufsize[.flag])

  与recv()类似,但返回值是(data,address)。其中data是包含接收数据的字符串,address是发送数据的套接字地址。

sk.send(string[,flag])

  将string中的数据发送到连接的套接字。返回值是要发送的字节数量,该数量可能小于string的字节大小。即:可能未将指定内容全部发送。

sk.sendall(string[,flag])

  将string中的数据发送到连接的套接字,但在返回之前会尝试发送所有数据。成功返回None,失败则抛出异常。

      内部通过递归调用send,将所有内容发送出去。

sk.sendto(string[,flag],address)

  将数据发送到套接字,address是形式为(ipaddr,port)的元组,指定远程地址。返回值是发送的字节数。该函数主要用于UDP协议。

sk.settimeout(timeout)

  设置套接字操作的超时期,timeout是一个浮点数,单位是秒。值为None表示没有超时期。一般,超时期应该在刚创建套接字时设置,因为它们可能用于连接的操作(如 client 连接最多等待5s )

sk.getpeername()

  返回连接套接字的远程地址。返回值通常是元组(ipaddr,port)。

sk.getsockname()

  返回套接字自己的地址。通常是一个元组(ipaddr,port)

sk.fileno()

  套接字的文件描述符

TCP:

 

import socketserver
服务端

class Myserver(socketserver.BaseRequestHandler):

def handle(self):

conn = self.request
conn.sendall(bytes("你好,我是机器人",encoding="utf-8"))
while True:
ret_bytes = conn.recv(1024)
ret_str = str(ret_bytes,encoding="utf-8")
if ret_str == "q":
break
conn.sendall(bytes(ret_str+"你好我好大家好",encoding="utf-8"))

if __name__ == "__main__":
server = socketserver.ThreadingTCPServer(("127.0.0.1",8080),Myserver)
server.serve_forever()

客户端

import socket

obj = socket.socket()

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

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

while True:
inp = input("你好请问您有什么问题? \\n >>>")
if inp == "q":
obj.sendall(bytes(inp,encoding="utf-8"))
break
else:
obj.sendall(bytes(inp, encoding="utf-8"))
ret_bytes = obj.recv(1024)
ret_str = str(ret_bytes,encoding="utf-8")
print(ret_str)

案例一 机器人聊天

复制代码
import  socketserver
服务端

class Myserver(socketserver.BaseRequestHandler):

    def handle(self):

        conn = self.request
        conn.sendall(bytes("你好,我是机器人",encoding="utf-8"))
        while True:
            ret_bytes = conn.recv(1024)
            ret_str = str(ret_bytes,encoding="utf-8")
            if ret_str == "q":
                break
            conn.sendall(bytes(ret_str+"你好我好大家好",encoding="utf-8"))

if __name__ == "__main__":
    server = socketserver.ThreadingTCPServer(("127.0.0.1",8080),Myserver)
    server.serve_forever()

客户端

import socket

obj = socket.socket()

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

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

while True:
    inp = input("你好请问您有什么问题? \\n >>>")
    if inp == "q":
        obj.sendall(bytes(inp,encoding="utf-8"))
        break
    else:
        obj.sendall(bytes(inp, encoding="utf-8"))
        ret_bytes = obj.recv(1024)
        ret_str = str(ret_bytes,encoding="utf-8")
        print(ret_str)
复制代码

服务端

import socket

sk = socket.socket()

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

while True:
conn,address = sk.accept()
conn.sendall(bytes("欢迎光临我爱我家",encoding="utf-8"))

size = conn.recv(1024)
size_str = str(size,encoding="utf-8")
file_size = int(size_str)

conn.sendall(bytes("开始传送", encoding="utf-8"))

has_size = 0
f = open("db_new.jpg","wb")
while True:
if file_size == has_size:
break
date = conn.recv(1024)
f.write(date)
has_size += len(date)

f.close()

客户端

import socket
import os

obj = socket.socket()

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

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

size = os.stat("yan.jpg").st_size
obj.sendall(bytes(str(size),encoding="utf-8"))

obj.recv(1024)

with open("yan.jpg","rb") as f:
for line in f:
obj.sendall(line)

案例二 上传文件

复制代码
服务端

import socket

sk = socket.socket()

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

while True:
    conn,address = sk.accept()
    conn.sendall(bytes("欢迎光临我爱我家",encoding="utf-8"))

    size = conn.recv(1024)
    size_str = str(size,encoding="utf-8")
    file_size = int(size_str)

    conn.sendall(bytes("开始传送", encoding="utf-8"))

    has_size = 0
    f = open("db_new.jpg","wb")
    while True:
        if file_size == has_size:
            break
        date = conn.recv(1024)
        f.write(date)
        has_size += len(date)

    f.close()

客户端

import socket
import os

obj = socket.socket()

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

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

size = os.stat("yan.jpg").st_size
obj.sendall(bytes(str(size),encoding="utf-8"))

obj.recv(1024)

with open("yan.jpg","rb") as f:
    for line in f:
        obj.sendall(line)
复制代码

UdP

import socket
ip_port = (\'127.0.0.1\',9999)
sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
sk.bind(ip_port)

while True:
data = sk.recv(1024)
print data

 


import socket
ip_port = (\'127.0.0.1\',9999)

sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
while True:
inp = input(\'数据:\').strip()
if inp == \'exit\':
break
sk.sendto(bytes(inp,encoding = "utf-8"),ip_port)

sk.close()

udp传输

复制代码
import socket
ip_port = (\'127.0.0.1\',9999)
sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
sk.bind(ip_port)

while True:
    data = sk.recv(1024)
    print data




import socket
ip_port = (\'127.0.0.1\',9999)

sk = socket.socket(socket.AF_INET,socket.SOCK_DGRAM,0)
while True:
    inp = input(\'数据:\').strip()
    if inp == \'exit\':
        break
    sk.sendto(bytes(inp,encoding = "utf-8"),ip_port)

sk.close()
复制代码

WEB服务应用:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#!/usr/bin/env python
#coding:utf-8
import socket
  
def handle_request(client):
    buf = client.recv(1024)
    client.send("HTTP/1.1 200 OK\\r\\n\\r\\n")
    client.send("Hello, World")
  
def main():
    sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    sock.bind((\'localhost\',8080))
    sock.listen(5)
  
    while True:
        connection, address = sock.accept()
        handle_request(connection)
        connection.close()
  
if __name__ == \'__main__\':
  main()

 

IO多路复用 

I/O(input/output),即输入/输出端口。每个设备都会有一个专用的I/O地址,用来处理自己的输入输出信息首先什么是I/O:

I/O分为磁盘io和网络io,这里说的是网络io

IO多路复用:

I/O多路复用指:通过一种机制,可以监视多个描述符(socket),一旦某个描述符就绪(一般是读就绪或者写就绪),能够通知程序进行相应的读写操作。

Linux

Linux中的 select,poll,epoll 都是IO多路复用的机制。

Linux下网络I/O使用socket套接字来通信,普通I/O模型只能监听一个socket,而I/O多路复用可同时监听多个socket.

I/O多路复用避免阻塞在io上,原本为多进程或多线程来接收多个连接的消息变为单进程或单线程保存多个socket的状态后轮询处理.

Python  

Python中有一个select模块,其中提供了:select、poll、epoll三个方法,分别调用系统的 select,poll,epoll 从而实现IO多路复用。

1
2
3
4
5
6
7
8
9
10
11
Windows Python:
 
    提供: select
 
Mac Python:
 
    提供: select
 
Linux Python:
 
    提供: select、poll、epoll

对于select模块操作的方法:

1
2
3
4
5
6
7
8
9
10
11
句柄列表11, 句柄列表22, 句柄列表33 = select.select(句柄序列1, 句柄序列2, 句柄序列3, 超时时间)
  
参数: 可接受四个参数(前三个必须)
返回值:三个列表
  
select方法用来监视文件句柄,如果句柄发生变化,则获取该句柄。
1、当 参数1 序列中的句柄发生可读时(accetp和read),则获取发生变化的句柄并添加到 返回值1 序列中
2、当 参数2 序列中含有句柄时,则将该序列中所有的句柄添加到 返回值2 序列中
3、当 参数3 序列中的句柄发生错误时,则将该发生错误的句柄添加到 返回值3 序列中
4、当 超时时间 未设置,则select会一直阻塞,直到监听的句柄发生变化
5、当 超时时间 = 1时,那么如果监听的句柄均无任何变化,则select会阻塞 1 秒,之后返回三个空列表,如果监听的句柄有变化,则直接执行。

import socket
import select


sk1 = socket.socket()
sk1.bind(("127.0.0.1",8001))
sk1.listen()

sk2 = socket.socket()
sk2.bind(("127.0.0.1",8002))
sk2.listen()

sk3 = socket.socket()
sk3.bind(("127.0.0.1",8003))
sk3.listen()

li = [sk1,sk2,sk3]

while True:
r_list,w_list,e_list = select.select(li,[],[],1) # r_list可变化的
for line in r_list:
conn,address = line.accept()
conn.sendall(bytes("Hello World !",encoding="utf-8"))

利用select监听终端操作实例

复制代码
import socket
import select


sk1 = socket.socket()
sk1.bind(("127.0.0.1",8001))
sk1.listen()

sk2 = socket.socket()
sk2.bind(("127.0.0.1",8002))
sk2.listen()

sk3 = socket.socket()
sk3.bind(("127.0.0.1",8003))
sk3.listen()

li = [sk1,sk2,sk3]

while True:
    r_list,w_list,e_list = select.select(li,[],[],1) # r_list可变化的
    for line in r_list: 
        conn,address = line.accept()
        conn.sendall(bytes("Hello World !",encoding="utf-8"))
复制代码

服务端:
sk1 = socket.socket()
sk1.bind(("127.0.0.1",8001))
sk1.listen()

inpu = [sk1,]

while True:
r_list,w_list,e_list = select.select(inpu,[],[],1)
for sk in r_list:
if sk == sk1:
conn,address = sk.accept()
inpu.append(conn)
else:
try:
ret = str(sk.recv(1024),encoding="utf-8")
sk.sendall(bytes(ret+"hao",encoding="utf-8"))
except Exception as ex:
inpu.remove(sk)

客户端
import socket

obj = socket.socket()

obj.connect((\'127.0.0.1\',8001))

while True:
inp = input("Please(q\\退出):\\n>>>")
obj.sendall(bytes(inp,encoding="utf-8"))
if inp == "q":
break
ret = str(obj.recv(1024),encoding="utf-8")
print(ret)

利用select实现伪同时处理多个Socket客户端请求

复制代码
服务端:
sk1 = socket.socket()
sk1.bind(("127.0.0.1",8001))
sk1.listen()

inpu = [sk1,]

while True:
    r_list,w_list,e_list = select.select(inpu,[],[],1)
    for sk in r_list:
        if sk == sk1:
            conn,address = sk.accept()
            inpu.append(conn)
        else:
            try:
                ret = str(sk.recv(1024),encoding="utf-8")
                sk.sendall(bytes(ret+"hao",encoding="utf-8"))
            except Exception as ex:
                inpu.remove(sk)

客户端
import socket

obj = socket.socket()

obj.connect((\'127.0.0.1\',8001))

while True:
    inp = input("Please(q\\退出):\\n>>>")
    obj.sendall(bytes(inp,encoding="utf-8"))
    if inp == "q":
        break
    ret = str(obj.recv(1024),encoding="utf-8")
    print(ret)
复制代码

服务端:
import socket
sk1 = socket.socket()
sk1.bind(("127.0.0.1",8001))
sk1.listen()
inputs = [sk1]
import select
message_dic = {}
outputs = []
while True:

r_list, w_list, e_list = select.select(inputs,[],inputs,1)
print("正在监听的socket对象%d" % len(inputs))
print(r_list)
for sk1_or_conn in r_list:
if sk1_or_conn == sk1:
conn,address = sk1_or_conn.accept()
inputs.append(conn)
message_dic[conn] = []
else:
try:
data_bytes = sk1_or_conn.recv(1024)
data_str = str(data_bytes,encoding="utf-8")
sk1_or_conn.sendall(bytes(data_str+"好",encoding="utf-8"))
except Exception as ex:
inputs.remove(sk1_or_conn)
else:
data_str = str(data_bytes,encoding="utf-8")
message_dic[sk1_or_conn].append(data_str)
outputs.append(sk1_or_conn)
for conn in w_list:
recv_str = message_dic[conn][0]
del message_dic[conn][0]
conn.sendall(bytes(recv_str+"好",encoding="utf-8"))
for sk in e_list:
inputs.remove(sk)

客户端:
import socket

obj = socket.socket()

obj.connect((\'127.0.0.1\',8001))

while True:
inp = input("Please(q\\退出):\\n>>>")
obj.sendall(bytes(inp,encoding="utf-8"))
if inp == "q":
break
ret = str(obj.recv(1024),encoding="utf-8")
print(ret)

利用select实现伪同时处理多个Socket客户端请求读写分离

复制代码
服务端:
import socket
sk1 = socket.socket()
sk1.bind(("127.0.0.1",8001))
sk1.listen()
inputs = [sk1]
import select
message_dic = {}
outputs = []
while True:

    r_list, w_list, e_list = select.select(inputs,[],inputs,1)
    print("正在监听的socket对象%d" % len(inputs))
    print(r_list)
    for sk1_or_conn in r_list:
        if sk1_or_conn == sk1:
            conn,address = sk1_or_conn.accept()
            inputs.append(conn)
            message_dic[conn] = []
        else:
            try:
                data_bytes = sk1_or_conn.recv(1024)
                data_str = str(data_bytes,encoding="utf-8")
                sk1_or_conn.sendall(bytes(data_str+"好",encoding="utf-8"))
            except Exception as ex:
                inputs.remove(sk1_or_conn)
            else:
                data_str = str(data_bytes,encoding="utf-8")
                message_dic[sk1_or_conn].append(data_str)
                outputs.append(sk1_or_conn)
        for conn in w_list:
            recv_str = message_dic[conn][0]
            del message_dic[conn][0]
            conn.sendall(bytes(recv_str+"好",encoding="utf-8"))
        for sk in e_list:
            inputs.remove(sk)

客户端:
import socket

obj = socket.socket()

obj.connect((\'127.0.0.1\',8001))

while True:
    inp = input("Please(q\\退出):\\n>>>")
    obj.sendall(bytes(inp,encoding="utf-8"))
    if inp == "q":
        break
    ret = str(obj.recv(1024),encoding="utf-8")
    print(ret)
复制代码

 

socketserver

SocketServer内部使用 IO多路复用 以及 “多线程” 和 “多进程” ,从而实现并发处理多个客户端请求的Socket服务端。即:每个客户端请求连接到服务器时,Socket服务端都会在服务器是创建一个“线程”或者“进程” 专门负责处理当前客户端的所有请求。

ThreadingTCPServer

ThreadingTCPServer实现的Soket服务器内部会为每个client创建一个 “线程”,该线程用来和客户端进行交互。

1、ThreadingTCPServer基础

使用ThreadingTCPServer:

  • 创建一个继承自 SocketServer.BaseRequestHandler 的类
  • 类中必须定义一个名称为 handle 的方法
  • 启动ThreadingTCPServer

import socketserver

class Myserver(socketserver.BaseRequestHandler):

def han

以上是关于用python进行socket编程老是报错error: [Errno 10061] 怎么回事啊??好心人帮帮忙啊!!在线等!谢了的主要内容,如果未能解决你的问题,请参考以下文章

Go语言之Go语言网络编程

Python socket编程

Python Socket 网络编程

python之socket编程

python之socket编程

Python学习 : Socket编程