Python Socket basic Usage

Source: Internet
Author: User

Sockets are also commonly referred to as sockets, which are used to connect to the server client and are a handle to a communication chain, where an application usually makes a request to the network through a socket or answers a network request.

Just like Python does with file:

File is open for one of the files--read-write-off

The socket is open for the server client--read-write-off

Server section:

Importsocket,sysserver_address=('127.0.0.1', 8000) Buf_size=1024Try: Server_sk=Socket.socket (socket.af_inet, socket. SOCK_STREAM) #创建新的socket对象exceptsocket.error, msg:Print "socket create fail, error code:"+ str (msg[0]) +'error message:'+ msg[1]Print 'Socket Create successfully'Try: Server_sk.bind (server_address) #绑定server地址, address depends on family of addresses, Af_inet is expressed in tuplesexceptsocket.error, msg:Print "socket bind address error:"+ str (msg[0]) +'Error Mssage:'+ msg[1]Print 'socket bind address successfully'Server_sk.listen (5)Print "Listening ..." whileTrue:client_sk, Client_addr=server_sk.accept ()Print "connected by:", Client_addr whileTrue:data=client_sk.recv (buf_size)Printdata Client_sk.sendall (data) server_sk.close ()
Parameter one: Address cluster socket.af_inet IPv4 (default) Socket.af_inet6 IPv6
Parameter two: type socket. Sock_stream streaming socket, for TCP (default) socket. SOCK_DGRAM datagram socket, for UDP
Client section:
Importsocket,sysserver_address=('127.0.0.1', 8000) Buf_size=1024Try: Client_sk=Socket.socket (socket.af_inet, socket. SOCK_STREAM)exceptsocket.error, msg:Print "socket create fail, error code:"+ str (msg[0]) +'error message:'+ msg[1]client_sk.connect (server_address) whileTrue:data= Raw_input ('Please input your sending message->')   if  notData:Print "input data cannot is empty, please input again ."      Continueclient_sk.sendall (data) Recv_data=client_sk.recv (buf_size)Printrecv_dataclient_sk.close ()

Server output:

Socket Create successfully
Socket BIND address successfully
Listening ...
Connected by: (' 127.0.0.1 ', 63388)
Aa

Client output:

Please input your sending MESSAGE->AA
Aa
Please input your sending message->

Python Socket basic Usage

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.