"Python Learning" socket programming

Source: Internet
Author: User


A basic component of network programming is socket (socket)

A socket is an instance of the socket class in a socket module, and his instantiation requires 3 parameters:

The first parameter is the address family (the default is socket.af_inet, the prefix af_ represents the address family (addr family))

The second parameter is a stream (socket. Sock_stream, default value) or datagram (socket. SOCK_DGRAM) sockets

The third parameter is the protocol, which defaults to 0 (typically not available)


# Importing Socket library: Import socket# When you create a socket, af_inet specifies that the IPV4 protocol is used, and IPV6 is designated as AF_INET6. SOCK_STREAM Specifies the use of a stream-oriented TCP protocol S = socket.socket (socket.af_inet, socket. SOCK_STREAM) # Establish a connection: Note that the parameter is a tuple that contains the address and port number. S.connect (' www.baidu.com ', 80)


Socket has two methods: Send and Recv


S.send (' get/http/1.1\r\nhost:www.sina.com.cn\r\nconnection:close\r\n\r\n ')


S.RECV: Call recv to receive data with a desired (maximum) number of bytes, general recommendation 1024



An example of a simple server side

Import sockets = Socket.socket () host = Socket.gethostname () #获取本机port = 2233s.bind ((host, Port)) S.listen (5) #调用listen The () method starts listening on the port, and the incoming parameter specifies the maximum number of pending connections: print ' Waiting for connection ... ' while True: #服务器通过循环来接受连接, accept () waits for and returns a client connection. Type is tuple:c,addr = s.accept () print ' Get connection from%s:%s '% addr c.send (' Thank-for-connecting ') C.close ()
# #升级版 # #使用面向对象的思维, a server based on the Socketserver class from socketserver import Tcpserver,streamrequesthandlerclass Handler ( Streamrequesthandler): def handle (self): addr = Self.request.getpeername () print ' Get connection from:%s%s '% add R Self.wfile.write (' Thank for Connection ') # "empty character indicates that the program is running Native server = TCPServer ((', 2345), Handler) server.serve_ Forever ()


An example of a client

Import sockets = Socket.socket () host = Socket.gethostname () port = 2233s.connect ((host, port)) print S.recv (2233)


"Python Learning" socket programming

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.