Python Network programming Learning socket socket

Source: Internet
Author: User
Tags set socket socket blocking

Previously also learned the socket programming, this time want to systematically study the use of Python network programming. Do this, for testing progress, also leave notes and footprints for the follow-up.

Socket common functions get host name and host address

Socket.gethostname ()

Socket.gethostbyname ()

Import Socket

host = Socket.gethostname ()
IP = socket.gethostbyname (host)
# can also resolve IP IP for external Services
= Socket.gethostbyname (' www.baidu.com ')

Interchange IPv4 address called 32-bit binary

Socket.inet_aton ()

Socket.inet_ntoa ()

Import Socket

Socket.inet_aton (' 127.0.0.1 ')
socket.inet_ntoa (' \x7f\x00\x00\x01 ')

Specify port and protocol find service name

Socket.getservbyport ()

Import Socket

# Look for HTTP and HTTPS services, such as no return notfound error
socket.getservbyport (' TCP ')
Socket.getservbyport (443, ' TCP ')

Inter-rotating host byte sequence and network byte order

Socket.ntohl ()/socket.ntohs ()

Socket.htonl ()/socket.htons () socket timeout

Socket.gettimeout ()

Socket.settimeout ()

Import Socket

# Set up a socket object
sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)

#  Timeout time defaults to empty
sock.settimeout (MB)
sock.gettimeout ()

Socket exception

Socket.error () General exception

Socket.gaierror () address exception

Socket.timeout () Timeout exception modify socket buffer

Socket.getsockopt ()

Socket.setsockopt ()

Import socket

sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)
# query buffer size
sock.getsockopt (socket. Sol_socket, SOCKET. SO_SNDBUF)
sock.getsockopt (socket. Sol_socket, SOCKET. SO_SNDBUF) 
# set size
sock.setsockopt (socket. Sol_socket, SOCKET. SO_SNDBUF, 2048)
sock.setsockopt (socket. Sol_socket, SOCKET. SO_SNDBUF, 2048) 
# set up reuse sockets

Set Socket blocking mode

Non-blocking: Socket encounters exception throws error socket.setblocking (0)

Blocking: Failure to encounter error does not prevent operation Socket.setblocking (1)

Get Network time server time (NTP proctol)

Import ntplib
import time

ntp = ntplib. NTPClient ()
res = ntp.request (' pool.ntp.org ')
print time.ctime (res.tx_time)

Practice socket client/server simple communication

Service side

Import Socket

# Initializes a socket
sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)

# SET SOCKET reusable
sock.setsockopt (socket. Sol_socket, SOCKET. SO_REUSEADDR, 1)

# bind socket
sock.bind ((' localhost ', 8888)

# Listen for client links, maximum number is 5
sock.listen (5) while

True:
    # Blocking, receiving
    client, add = sock.accept ()
    data = CLIENT.RECV (2048)
    print "Data:", data, ' \ n ', ' Host: ' , add, ' \ n '
    # close
    client.close 

Client

Import Socket

# Initializes a socket
sock = Socket.socket (socket.af_inet, socket. SOCK_STREAM)

# Link service-side
sock.connect ((' localhost ', 8888))

try:
    # Send data
    sock.sendall ("from Client socket! ")

Except Socket.error, E:
    print "error!"

Finally:
    # close
    sock.close ()

Two terminal communications

Service side

duck@duck:~/sockdir/chapter_1$ python sersock.py 
data:from client socket! 
Host: (' 127.0.0.1 ', 53910)
Client

duck@duck:~/sockdir/chapter_1$ python clisock.py

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.