Python socket module

Source: Internet
Author: User

Network-based sockets: Af_inet has two protocols, connection-oriented protocol Tcp:sock_strem,
Non-connected Protocol Udp:sock_dgram

Service side:
Socket.socket (): Create socket Socket.af_inet,socket. Soct_stream
Socket.bind (): Bind address (IP address and port number) to socket
Socket.listen (): Start TCP Listener
Socket.accept (): Passive Accept client connection, (blocking) waiting for connection to arrive

A simple service-side

#Coding:utf-8Importsocket, time#Create a TCP connection based on a network sockets=Socket.socket (Socket.af_inet,socket. Sock_stream) S.bind (('0.0.0.0', 1234) ) S.listen () whileTrue:Print 'start ...'sockobj, addr=s.accept ()Print 'Client IP:', addr whileTrue:data=SOCKOBJ.RECV (1024)        if  notData: Breaksockobj.send (str (time.time ())) Sockobj.close () s.close ()

Client

Socket.connect (): Active initialization of the server connection
SOCKET.CONNECT_EX (): Extended version of Connect, returning an error code instead of throwing an exception when an error occurs

# Coding:utf-8 Import Socket Host= ('192.168.1.102', 1234) c=Socket.socket (socket.af_ INET, Socket. SOCK_STREAM) C.connect (host) while True:    mesg=raw_input ("send>:  ")    c.send (MESG)    data=c.recv (1024x768)    print  Data    C.cloce ()

Python socket module

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.