Python module: Socket module

Source: Internet
Author: User

1.Socket type

Socket (family,type[,protocal]) to create a socket using the given address family, socket type, protocol number (default is 0)

Socket type
Describe
Socket.af_unix

can only be used for a single inter-process communication between Unix systems

Socket.af_inet
network communication between servers
Socket.af_inet6

IPv6

Socket. Sock_stream
streaming socket, for TCP
Socket. Sock_dgram
datagram socket, for UDP
socket. Sock_raw
IGMP and other network messages, and Sock_raw Yes; second, Sock_raw message; In addition, using the original socket, you can ip_hdrincl ip< Span style= "font-size:16px;padding:0px;margin:0px;font-family: ' The song Body '; > header.
Socket. Sock_seqpacket
Reliable, continuous packet service
Create a TCP Socket
s= Socket.socket (Socket.af_inet,socket. SOCK_STREAM)
Create a UDP Socket
S=socket.socket (Socket.af_inet,socket. SOCK_DGRAM)


2.Socket function

Socket function (server)
Describe
S.bind (Address)
binds a socket to an address , and under af_inet, the address is represented as a tuple (host,port) .

start listening Tcp Specifies the maximum number of connections that the operating system can suspend before rejecting the connection. This value is at least 1, Most applications are set to 5

S.accept ()
Accept TCP Connects and returns (conn,address), where Conn is a new socket object that can be used to receive and send data. address is the location of the connection client.
Socket function (client)
s.connect (address) Address hostname,port error.
s.connect_ex (adddress)

function and connect (address) Errno

public Socket

s.recv ( Bufsize[,flag])

accept Tcp data for the socket. The data is returned as a string, Bufsize Big Data volume Flag provides additional information about the message, which can usually be ignored.

s.send ( String[,flag])

send Tcp data. String

s.se Ndall (String[,flag])

full send Tcp data. String failure throws an exception.

S.close () Close the socket.
S.gettimeout ()

returns the value of the current timeout, in seconds, or if no over-time is set . None.

S.fileno () Returns the file descriptor for the socket.
S.makefile ()

Create a file associated with the socket


3.socket principle

TCP Service side:

1. Create sockets, bind sockets to local IP with Port (socket.socket (socket.af_inet,socket). SOCK_STREAM) , S.bind ())

2. start the Listening connection (S.listen ())

3. Enter the loop and continue to accept client connection requests (S.accept ())

4. then receive the transmitted data, and send the data (S.RECV (), S.sendall ())

5. when the transfer is complete, close the socket (S.close ())


TCP Client:

1. Create build sockets, connect to the remote address (socket.socket (socket.af_inet,socket. SOCK_STREAM) , S.connect ())

2. send data and receive data after connection (S.sendall (), S.RECV ())

3. when the transfer is complete, close the socket (S.close ())


4. Example 1:

Service side:

[Email protected] python]# vim socket12.py
#!/bin/env python
#!-*-Coding:utf-8-*-
Import socket

S=socket.socket ()
Host=socket.gethostname ()
port=1234
S.bind ((Host,port))

S.listen (5)
While True:
C,addr=s.accept ()
print "Got connection from,addr"
C.send ("Thank for Connection")
C.close ()
[Email protected] python]# chmod 755 socket12.py
[Email protected] python]#./socket12.py

[Email protected] python]# Netstat-antulp | grep 1234
TCP 0 0 0.0.0.0:1234 0.0.0.0:* LISTEN 31580/python


Client:

[Email protected] ~]# vim socket12.py

#!/bin/env python
#!-*-Coding:utf-8-*-
Import socket

S=socket.socket ()
Host= ' 0.0.0.0 '--IP address on the server
port=1234

S.connect ((Host,port))
Print S.RECV (1024)

[Email protected] ~]# chmod 755 socket12.py
[Email protected] ~]#./socket12.py
Thank for connection
[Email protected] ~]#


At this point, the server will have data:

650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/7F/42/wKioL1cYR2uxff34AAAJkT17rUU679.png "title=" 1.png " alt= "Wkiol1cyr2uxff34aaajkt17ruu679.png"/>

This article is from the "Days Together" blog, please be sure to keep this source http://tongcheng.blog.51cto.com/6214144/1766143

Python module: Socket module

Related Article

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.