the #Handling Connection Socket Exceptions the Try: the S.connect ((host, port)) + exceptSocket.gaierror as E: - Print("address-related Error connecting to server:%s"%e) theSys.exit (1)Bayi #third Try-except block--Sending data the #Handling Send data errors the Try: - #get method sent to Web site -data ="GET%s http/1.0\r\n\r\n"%filename the S.sendall (Data.encode ()) the exceptSocket.error as E: the
This article mainly introduces the example of UDP port multiplexing in the Python Socket programming process. The author used the Python twisted asynchronous framework. If you need it, refer
About Port multiplexing
A socket cannot bind multiple ports at the same time. If th
Because the project requires a lot of data, even though databases released by some research centers on the Internet are charged, it is a luxury for me to have three employees.
(A human face recognition database requires 1000 million USD! WTF !)
Try to develop a search tool by yourself...
First test a small socket function (very small, the experts will pass ...)
[Python] # testint of sending infomation to
, 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
= 80TenREMOTE_IP =Socket.gethostbyname (host) One #get the IP address of the host, gethostbyname is the IP address of the Web page you do not know, you can obtain a URL, generally on their own computer is not used A Printremote_ip - S.connect ((remote_ip,port)) -Message ='get/http/1.1\r\n\r\n' the #This is a command for HTTP request Web page content - s.sendall (message) - Print 'Message Send successfully' -Reply = S.recv (4096) + Printreply -S.close ()Server and client are a
This article only cares about Python's network programming and focuses on TCP/IP units.First, a simple understanding of the following:1, what is TCP/IP,UDP?TCP/IP (transmission Control protocol/internet Protocol) is a protocol/inter-network protocol, an industry-standard set of protocols designed for wide area networks (WANs).UDP (user Data Protocol, Subscriber Datagram Protocol) is the protocol that corresponds to TCP. It is a part of the TCP/IP prot
To compile a server in python, follow these steps:
1.
The first step is to create a socket object. Call the socket constructor. For example:
Socket = socket. socket (family, type)
The family parameter represents the address family
The server is very similar to the client program. The client program is learned in the previous section. This section describes how to use socket to establish TCP and UDP servers.
1. TCP connection establishment method
Generally, two steps are required for the client to establish a TCP connection, and four steps are required for the server process. for details, see the comparison below.
Procedure
TCP client
TCP server
Step 1
"Everything is file", and the file is operated with "open" "Read and Write" "Off" mode. Socket is an implementation of this pattern, the socket is a special kind of file, some of the socket function is the operation of it (read/write Io, open, close)The difference between a socket and file:
The file module is
1. TCP connection Establishment Method
Generally, two steps are required for the client to establish a TCP connection, and four steps are required for the server process. For details, see the comparison below.
Procedure
TCP client
TCP Server
Step 1
CreateSocketObject
CreateSocketObject
Step 2
CallConnect ()Establish a connection with the server
SetSocketOptional)
Step 3
None
Bind to a port (or a specified Nic)
Step 4
None
. Sk.recvfrom (Bufsize[.flag]) is similar to recv (), but the return value is (data,address). Where data is the string that contains the received information, address is the socket addressing that sent the data. Sk.send (String[,flag]) sends data from a string to a connected socket. The return value is the number of bytes to send, which may be less than the byte size of the string. That is, the specified co
=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
Python-based socket Programming (III)What can be achieved with socket-based communication implemented earlier? In the TCP protocol communication is a user said a sentence, the service side to you back, you give the service side to say, the service side to give you back a sentence, so always friendly play down. And so o
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
1, the establishment of TCP connection method
The client typically takes two steps to establish a TCP connection, and the server process takes four steps, as shown in the following comparison.
Steps
TCP Client
TCP Server
The first step
Set up a socket Object
Establish Socket Object
Step Two
Call Connect () to establish a connection to the server
S
Construct IP and ICMP headers for Python socket programming, pythonicmp
In the past two days, an experiment requires you to construct your own IP Address Header. If you encounter many problems, you have finally completed the experiment in one day.
There are a lot of questions about socket on the Internet. I only record
In the experiment, you need to construct a separate HTTP data packet, and use SOCK_STREAM to send data packets requires complete TCP interaction. Therefore, we want to use the original socket for programming, construct data packets directly, and send data at the IP layer, that is, use SOCK_RAW for data transmission. The advantage of SOCK_RAW is that it can completely modify the data packets, process all dat
1. Method of establishing TCP connection
The client typically takes two steps to establish a TCP connection, and the server process takes four steps, as shown in the comparison below.
Steps
TCP Client
TCP Server
First step
Create a socket object
Create a socket object
Second Step
Call Connect () to establish a connection with the server
To set
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.