Python network programming-TCP and UDP socket usage example, pythonudp
This article describes the use of TCP and UDP socket in Python network programming. We will share this with you for your reference. The details are as follows:
TCP protocol
Server:
#! /Usr/bin/env pythonfrom soc
43, the Python module socket:Python is powerful in network communication, learn the basic way of socket communicationUDP communication:Server:Import Socketport=8081s=socket.socket (Socket.af_inet,socket. SOCK_DGRAM) #从指定的端口, from any sender, receives the UDP data S.bind ((", port)) print (' Waiting for access ... ') while True: #接收一个数据 data,addr= S.recvfrom
Python full stack "socket network programming"
The contents of this chapter:
Socket
IO multiplexing (select)
Socketserver Module (threadingtcpserver Source Analysis)
Socket
Sockets are also commonly referred to as socketUsed to describe the IP address and por
");Socket.onopen = function () {Alert ("Connection established.");};Socket.onerror = function () {Alert ("Connection error.");};Socket.onclose = function () {Alert ("Connection closed.");};In these three events, only the event object for the Close event has additional information. The event object for this event has three additional attributes: Wasclean, code, and reason. Where Wasclean is a Boolean value indicating whether the connection has been explicitly closed; code is the numeric status co
, general recommendation 1024An example of a simple server sideImport sockets = Socket.socket () host = Socket.gethostname () #获取本机port = 2233s.bind ((host, Port)) S.listen (5) #调用listen The () method starts listening on the port, and the incoming parameter specifies the maximum number of pending connections: print ' Waiting for connection ... ' while True: #服务器通过循环来接受连接, accept () waits for and returns a client connection. Type is tuple:c,addr = s.accept () print ' Get connection from%s:%s '% a
shut down the server)#ser_socket.close ()TCP Service SideTCP Service Side fromSocketImportsocket, af_inet, SOCK_STREAMIP='127.0.0.1'PORT= 8888ADDRESS=(IP, PORT) BUFSIZE= 1024#1. Create a client socket objectCli_socket =socket (af_inet, sock_stream)#2. Connect to the serverCli_socket.connect (ADDRESS)#3. Data collectionCli_socket.send ('Hello'. Encode ('Utf-8'))#can only be sent as a byte typedata =cli_sock
()
Returns the hostname.
The following is an example of a simple socket communication, including a socket server to listen to a local server port, a socket client sends a request to the server side. A message is returned after a request is received by the server sideSocket Server SideCreated a simple socket
Python provides two basic socket modules.The first one is the Socket, which provides the standard BSD Sockets API.The second is Socketserver, which provides a server-centric class that simplifies the development of Web servers.1. Socket data typeThe socket type is the same a
Python uses Raw Socket for Ethernet frame injection and pythonraw1. Frame Injection
For introduction to Ethernet frames and Raw sockets, refer to a previous blog post that uses Raw Socket for Ethernet frame sniffing. Frame injection features:
Packages in the original data format can be injected into the network.
It is very powerful in simulating network resp
=socket.socket (Socket.af_inet,socket. sock_raw,255)S.setsockopt (0, socket. IP_HDRINCL, 1)If this is set under OS X, the invalid argument error is reported in the location of the call to SendTo (), and then the problem is found on the third parameter 255 of the first line, which is testedUnder OS X, this parameter is set to 0 or 255 to be an error.Under Windows, this parameter is set to 0 or 255 without er
A detailed explanation of the handshake between the client and the server of python Socket, pythonsocket
In simple learning, socket is used to establish a connection between the client and the server and send data.
1. Client socketClient. py code
Import socket s = socket.
packet service
socket.af_unix
3, Connection Server Client.connect ((' www.baidu.com ') #使用元组tuple的方式传递两个参数 4, send data client.send (b ' Hello ') #传送的数据必须为字节的方式 5, receiving data = CLIENT.RECV (1024x768) #每次接收1K数据, data 6, close Socketclient.close () server side: 1, select protocol: Server = Socket.socket () 2, listening port: server.bind ((' localhost ') # Using tuple tuples to pass the Server.listen (3) #监听端口, and set the maximum number of waiting con
On the server side, socket () returns the socket for listening (listen) and accept, which cannot be used to send and receive data between clients.Accept () accepts a connection request from a client and returns a new socket that differs from the socket () that is returned by the so
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.