`

Python socket简介

 
阅读更多
自豪地使用dir和help.
Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
[GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import socket

>>> dir(socket.socket)
['__class__', '__delattr__', '__doc__', '__format__', '__getattribute__', '__hash__', '__init__', '__module__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__slots__', '__str__', '__subclasshook__', '__weakref__', '_sock', 'accept', 'bind', 'close', 'connect', 'connect_ex', 'dup', 'family', 'fileno', 'getpeername', 'getsockname', 'getsockopt', 'gettimeout', 'listen', 'makefile', 'proto', 'recv', 'recv_into', 'recvfrom', 'recvfrom_into', 'send', 'sendall', 'sendto', 'setblocking', 'setsockopt', 'settimeout', 'shutdown', 'type']

>>> help(socket.socket)

class _socketobject(__builtin__.object)
 | socket([family[, type[, proto]]]) -> socket object
 |  
 | Open a socket of the given type. The family argument specifies the
 | address family; it defaults to AF_INET. The type argument specifies
 | whether this is a stream (SOCK_STREAM, this is the default)
 | or datagram (SOCK_DGRAM) socket. The protocol argument defaults to 0,
 | specifying the default protocol. Keyword arguments are accepted.
 |  
 | A socket object represents one endpoint of a network connection.
 |  
 | Methods of socket objects (keyword arguments not allowed):
 |  
 | accept() -- accept a connection, returning new socket and client address
 | bind(addr) -- bind the socket to a local address
 | close() -- close the socket
 | connect(addr) -- connect the socket to a remote address
 | connect_ex(addr) -- connect, return an error code instead of an exception
 | dup() -- return a new socket object identical to the current one [*]
 | fileno() -- return underlying file descriptor
 | getpeername() -- return remote address [*]
 | getsockname() -- return local address
 | getsockopt(level, optname[, buflen]) -- get socket options
 | gettimeout() -- return timeout or None
 | listen(n) -- start listening for incoming connections
 | makefile([mode, [bufsize]]) -- return a file object for the socket [*]
 | recv(buflen[, flags]) -- receive data
 | recv_into(buffer[, nbytes[, flags]]) -- receive data (into a buffer)
 | recvfrom(buflen[, flags]) -- receive data and sender's address
 | recvfrom_into(buffer[, nbytes, [, flags])
 | -- receive data and sender's address (into a buffer)
 | sendall(data[, flags]) -- send all data
 | send(data[, flags]) -- send data, may not send all of it
 | sendto(data[, flags], addr) -- send data to a given address
 | setblocking(0 | 1) -- set or clear the blocking I/O flag
 | setsockopt(level, optname, value) -- set socket options
 | settimeout(None | float) -- set or clear the timeout
 | shutdown(how) -- shut down traffic in one or both directions

>>> help(socket.getaddrinfo)

Help on built-in function getaddrinfo in module _socket:

getaddrinfo(…)
    getaddrinfo(host, port [, family, socktype, proto, flags])
        -> list of (family, socktype, proto, canonname, sockaddr)
    
    Resolve host and port into addrinfo struct.



Simple Demo:
# Echo server program
import socket

HOST ='' # Symbolic name meaning all available interfaces
PORT =50007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print'Connected by', addr
while 1:
    data = conn.recv(1024)
    if not data: break
    conn.sendall(data)
conn.close() 


# Echo client program
import socket

HOST ='' # The remote host
PORT =50007 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((HOST, PORT))
s.sendall('Hello, world')
data = s.recv(1024)
s.close()
print'Received', repr(data)



1、setblocking(0 | 1) -- set or clear the blocking I/O flag
引用
Set blocking or non-blocking mode of the socket: if flag is 0, the socket is set to non-blocking, else to blocking mode. Initially all sockets are in blocking mode. In non-blocking mode, if a recv() call doesn’t find any data, or if a send() call can’t immediately dispose of the data, a error exception is raised; in blocking mode, the calls block until they can proceed. s.setblocking(0) is equivalent to s.settimeout(0.0); s.setblocking(1) is equivalent to s.settimeout(None).

2、setsockopt(level, optname, value) -- set socket options
引用
Set the value of the given socket option (see the Unix manual page setsockopt(2)). The needed symbolic constants are defined in the socket module (SO_* etc.). The value can be an integer or a string representing a buffer. In the latter case it is up to the caller to ensure that the string contains the proper bits (see the optional built-in module struct for a way to encode C structures as strings).

套接字机制提供两个套接字选项接口来控制套接字行为。一个用来设置,一个用来获取。(getsockopt)
3、settimeout(None | float) -- set or clear the timeout
引用
Set a timeout on blocking socket operations. The value argument can be a nonnegative float expressing seconds, or None. If a float is given, subsequent socket operations will raise a timeout exception if the timeout period value has elapsed before the operation has completed. Setting a timeout of None disables timeouts on socket operations. s.settimeout(0.0) is equivalent to s.setblocking(0); s.settimeout(None) is equivalent to s.setblocking(1).

4、shutdown(how) -- shut down traffic in one or both directions
引用
Shut down one or both halves of the connection. If how is SHUT_RD, further receives are disallowed. If how is SHUT_WR, further sends are disallowed. If how isSHUT_RDWR, further sends and receives are disallowed. Depending on the platform, shutting down one half of the connection can also close the opposite half (e.g. on Mac OS X, shutdown(SHUT_WR) does not allow further reads on the other end of the connection).


资料参考:
http://docs.python.org/2/library/socket.html
分享到:
评论

相关推荐

    python:socket传输大文件示例

    在Python编程中,Socket是网络通信的基本模块,用于实现不同计算机之间的数据传输。本文将深入讲解如何使用Python的Socket库来传输大文件。文件传输在许多应用中都是必不可少的,例如分布式系统、文件共享和备份等。...

    python socket 网络编程

    Python的socket库是进行网络通信的基础模块,它提供了低级别的、原始的套接字接口,使得程序员能够构建自己的网络应用程序,如服务器和客户端。在"python socket 网络编程"这个主题中,我们将深入探讨如何利用socket...

    Python Socket模块实现服务端与客户端通信

    Python Socket模块是网络编程的基础,它提供了低级别的、原始的TCP/IP套接字接口,用于在客户端和服务器之间建立通信。Socket模块使得Python程序员能够轻松地处理网络通信,包括创建和管理TCP、UDP等不同协议的连接...

    python socket

    Python的socket模块是网络编程的基础,它提供了低级的、原始的TCP/IP和UDP套接字接口。在本文中,我们将深入探讨Python中socket的基本用法、如何创建服务器和客户端,以及涉及的一些重要概念。 首先,理解socket的...

    Python Socket

    Python Socket是Python编程中用于网络通信的一个模块,它允许程序创建和使用套接字(socket)来发送和接收数据。套接字是网络上的进程间通信(IPC)的基本接口,可以实现不同计算机之间的数据传输。在Python中,我们...

    基于python的socket实现单机五子棋到双人对战

    ### 基于Python的Socket实现单机五子棋到双人对战 #### 概述 本文将详细介绍如何利用Python中的socket库实现一个简单的五子棋游戏,并将其从单机版扩展为支持两人在线对战的版本。通过这个过程,我们将学习到Python...

    Dos.rar_nine44k_python_python socket_python socket聊天_python聊天室

    本项目" Dos.rar_nine44k_python_python socket_python socket聊天_python聊天室"正是基于Python的socket模块实现的一个多人在线聊天室,让我们来深入探讨这个话题。 首先,我们要理解什么是Python的socket。Socket...

    python socket 石头剪刀布游戏_socket_python_MYSQL_

    在Python编程领域,Socket是用于网络通信的一种基础模块,它允许程序通过网络发送和接收数据。在这个项目中,我们利用Python的socket库构建了一个石头剪刀布(RPS,Rock-Paper-Scissors)游戏,同时结合了MySQL...

    python socket文件接收代码

    文件接收代码 python socket编程,通过修改IP和port来进行交互

    Python Socket多人聊天编程

    在IT领域,网络编程是构建分布式系统和互联网应用程序的基础,而Python作为一种强大的脚本语言,提供了简洁易用的socket库来处理网络通信。本项目"Python Socket多人聊天编程"是基于Python实现的一个简单的多用户...

    Python 通过 socket 实现服务端和客户端相互间网络通信的例子

    在Python编程语言中,`socket`模块是进行网络通信的基础工具。它允许程序创建和使用网络连接,使得服务器端和客户端能够交换数据。本例中,我们探讨的是如何使用Python的socket库创建一个简单的服务端(server.py)...

    python socket客户端

    python socket客户端

    利用Python SOCKET多线程开发FTP软件

    ### 利用Python SOCKET多线程开发FTP软件 在当今高度数字化的世界中,文件传输协议(FTP)作为互联网上的一项标准服务,被广泛用于在网络上进行文件传输。利用Python的socket库和多线程技术开发一个FTP软件,不仅...

    python socket局域网聊天与文件传输.zip

    "python socket局域网聊天与文件传输.zip" 这个标题表明了我们讨论的主题是基于Python的socket编程技术,用于构建一个局域网内的聊天和文件传输应用。Socket是网络编程的基础,它允许两个或多个设备通过网络进行通信...

    Python Socket传输文件示例

    发送端可以不停的发送新文件,接收端可以不停的接收新文件。 例如:发送端输入:e:\visio.rar,接收端会...s=socket.socket(socket.AF_INET,socket.SOCK_STREAM) #定义socket类型 s.bind((host,port)) #绑定需要监听的I

    02.6.利用Python SOCKET多线程开发FTP软件(全6集)

    #### 1.1 Socket简介 在计算机网络中,Socket是一种用于进程间通信的机制,它允许应用程序通过网络发送和接收数据。Socket API(应用程序编程接口)提供了一种标准的方法来创建网络连接,并进行数据交换。在Python中...

    python.rar_python_python socket_python socket_python编程_socket

    python简介,使用_Python_进行_socket_编程

    python socket 网络编程 教程

    Python网络编程教程主要介绍如何使用Python语言开发基于socket的网络应用程序,通过本教程的学习,初学者可以掌握Python在网络编程方面的基础知识和高级特性。本教程采用的示例为聊天程序,旨在通过实例加深对Python...

    python;Socket接口;测试脚本直接运行

    在IT行业中,Python是一种广泛应用的开发语言,尤其在网络编程领域,它的Socket接口扮演着重要角色。Socket接口是网络通信的基础,它允许程序通过网络发送和接收数据,从而实现不同计算机之间的通信。在这个主题中,...

Global site tag (gtag.js) - Google Analytics