(waiting for the data is ready)----The kernel waits for a readable(2) Copy the data from the kernel into the process (Copying the data from the kernel to the process)----Copy the kernel readIt is important to remember these two points because the difference between these IO model is that there are different situations in both phases.1.3What are I/O operations?
network Operation , that is, the establishment of the socket object, to establish
, sending all the content.
Sk.sendto (string[,flag],address)
Sends the data to the socket, address is a tuple in the form of (Ipaddr,port), specifying the remote address. The return value is the number of bytes sent. This function is mainly used for UDP protocol.
Sk.settimeout (Timeout)
Sets the timeout period for the socket operation, and timeout is a floating-point nu
error that throws a specified type on its own.Try : = Input ("). Strip () = Int (n) + 1 raise TypeError except TypeError: Print ('----raise throws the Type Error---------') execution result: D:\Python\Python36-32\ Python.exe e:/python/day-23/day23.py>>>: 1----raise the Type Error thrown--------- Process finished with exit code 0Custom Exception classes:class Ldslyerror (baseexception): #定
1. Socket base
There are two ways to connect the client and the server: TCP and UDP,TCP are connection-oriented (three handshake, four wave, etc.), reliable but resource-consuming, but UDP is not connected, unreliable but fast. There's a lot of learning, but most people know that's enough.
2. A simple TCP example (blocking mode)
In both Python and other languages, so
The native socket client is blocked when it connects to the server, that is, when the server calls the Accept method, while the server and the client are blocked when the data is received (called recv). The native socket server can only handle one client request at a time, that is, the server cannot communicate with multiple clients simultaneously, and the service side resource is idle (the server only occu
communicate via the Send and Recv methods"""Connection.settimeout (5) Buffer= CONNECTION.RECV (1024) ifBuffer = ='1': Connection.send ('Welcome to server!') Else: Connection.send ('Cut off the connection!') exceptsocket.timeout:Print('Time out!') """finally the transfer ends, the server calls the socket's Close method to close the connection"""connection.close ()#---------------------------------------------------------------------- defrun_
there is a "distributed System" this book, if you are blocking calls, you will always put yourself "hang" until the book has no results, if the non-blocking call, you don't care if the boss has told you, you have to play the first side, Of course, you'll have to check the boss for a few minutes occasionally.Here the blocking is independent of the non-blocking and whether synchronous asynchronous. It has nothing to do with how the boss responds to your results.2. Use forkingmixin in the
Server-side socket_server.py file#_ *_ coding:utf-8 _*_# Importing Socket module Import socket# Create socket Object SK = Socket.socket () #绑定侦听的IP和端口号ip_port = (' 192.168.9.213 ',9999) Sk.bind (ip_port) #最大连接数sk. Listen (5) #接受请求, the client's socket object can be obtained when the request is received, and the client'
Directly on the code, then explain:#-*-Coding:utf-8-*-# Maybe you don't want to write process-based applications for some reason, rather than write multithreaded applications# As with previous Forkingmixin-based socket servers, socket servers written with ThreadingMixIn are subject to the same ECHO server programming pattern# Threadedservr inherits from TCPServer
protocol:Service sideImportSocketsk= Socket.socket (type=socket. SOCK_DGRAM)#Create a UCP socket objectSk.bind (('127.0.0.1', 8080))#binding IP and Portsmsg, Address= Sk.recvfrom (1024)#The UCP waiting to receive data must first receive dataPrint(Msg.decode ('Utf-8')) Sk.sendto (b'Hello', address)#send data to carry the address where the data is sentsk.close ()ClientImportSocketsk= Socket.socket (type=
123456789
#-*-coding:utf-8-*- import socket print "Creatingsocket" s = socket.socket (socket.af_inet, socket. SOCK_STREAM) print "done!" print "connectingto Remotehost " s.connect (( , 80 )" print "Done"
When Python uses the Socket.af_inet constant after using the import socket, this co
Both sides of the communication need to have a server and a client, so separate to write code.So I created two PY programs, the first one is the server: iserver.py and client iclient.pyService side:#coding: Utf-8From socket Import *s = socket (af_inet,sock_stream) #建立一个服务器socket对象, similar to the process of buying a phone, af_inet is the
here", encoding= ' Utf-8 '))"""This is the service side of socket network programming."""Import socketObj=socket.socket ();Obj.connect (("172.8.250.59", 8008))RECIVE=OBJ.RECV (1024x768) #最多接收1024字节Recive=str (recive,encoding= ' utf-8 ')Print (recive)While True: Inp=input ("Please enter what you want to send") If inp== ' Q ': Obj.sendall (Bytes (INP, encoding= ' Utf-8 ')) Break Else
, respectively, where clients can run multiple.
Both the server side and the client use the socket and thread(thread), which is written in an object-oriented way.
Client.py is relatively simple. The analysis is as follows: first the nickname information is initialized and the corresponding port is monitored. The member function in main declares two threads, one for sending information and one for receiving information. When no information is rec
(1) simple link with socketthe socket inside Python supports UDP, TCP, and interprocess communication . the socket can encapsulate what we want to send, send the past, and then back to the original appearance, in fact, network communication can understand that Chengdu is built on the socket, the following code is to de
Python TCP Socket programming detailed
Beginner script Language Python, test the available TCP communication programs:
Server:
#!/usr/bin/env python #-*-coding:utf-8-*- Import Socket import Threading import Time def tcplink (
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.