1, half open socket
Using the shutdown () function, the bidirectional data transmission of the socket becomes one-way data transfer. Shutdown () requires a separate parameter that indicates how to close the socket. Specific: 0 means to prohibit future reading; 1A prohibition of future writing; 2 means no future reading and writing.
2, Timeouts control timeout
()-Timeout4 5 Returns The timeout in seconds (float) associated with socket6 operations. A timeout of None indicates that timeouts on socket7 operations is disabled.8 """9 returnTimeoutTen One A defsettimeout (self, timeout):#real signature unknown; restored from __doc__ - """ - settimeout (Timeout) the - Set A timeout on socket operations. ' Timeout ' can be a float, - giving in seconds, or None. Setting a timeout of None disables - T
, message = b'Link'): Sckt=Socket.socket (socket.af_inet, socket. Sock_stream) Sckt.connect ((Self.host, Self.port)) sckt.send (message)"""data Unpacking, note that unpack requires four bytes, which is used after the following [: 4] """Open= Struct.unpack ('F', SCKT.RECV (1024) [: 4]) sckt.close ()"""return open price, go to format usage followed by [0]"""
returnOPEN[0]
2. f2.py
# -*-coding:utf-8-*-
Import sys
sys.path.append (
In Python socket programming, TCP and UDP programming is used. The following two different programming methods are used to send information from the client to the server,
The server returns the current time and received information of the customer service.
Let's first imp
races), which are file-based and web-based.Socket family based on file type: Socket family name: Af_unixUnix everything is file, the file-based socket calls the underlying file system to fetch data, two socket processes run on the same machine, you can access the same file system to complete the communication indirectly.Socket family based on network type:
Server
#! /Usr/bin/env python
Import socket
Import time
Host =''
Port = 99999
S = socket. socket (socket. AF_INET, socket. SOCK_STREAM)
S. bind (host, port ))
S. listen (2)
While 1:
Conn, addr = s. accept ()
#---------------
Print
The content of this section:
Exception handling
Socket syntax and related
Socketserver for multiple concurrency
Introduction to Processes and threads
Threading Instances
Thread lock, GIL, Event, semaphore
Producer Consumer Model
Example of traffic lights and steamed buns
Multiprocess instances
Inter-process communication
Queues Queue
SOURCE Directory Link: http://www.cnblogs.com/alex3714/articles/5227251.htmlJob 1
parameters, family represents the family. Type is the default TCPSocket.bind (): Server side, accepted parameters are tuples, not passed host,port two elements, but (Host,port), bound host, port number to socketSocket.listen (): A parameter that allows the client to connect to the server's queued line, if it exceeds the pending upper limit will be rejected, start listening to the bound port numberSocket.accept (): No parameter, its return value is a tuple, the first is a new
1. Half Open socket
Use the shutdown () function to make the socket bidirectional data transfer into one-way data. Shutdown () requires a separate parameter that indicates how to close the socket. The specific: 0 means prohibition of future reading; 1to prohibit future writing; 2 means to prohibit future reading and writing.
2. Timeouts Control timeout
Call the
],'is listening on port.')if __name__=='__main__': Parser= Argparse. Argumentparser (Description ='Try connecting to Port') parser.add_argument ('hostname', help='hostname This want to the contact') connect_to (Parser.parse_args (). hostname)The getaddrinfo () function is used to convert a user-specified host name and port number to an important method that is required for the address used by the socket method. For example, use getaddrinfo () to get a
# server.pyimport socketimport threadingimport times = socket.socket(socket.AF_INET,socket.SOCK_STREAM)s.bind(('127.0.0.1',9999))s.listen(5)print('Waiting connection...')def tcp_link(sock,addr): print('Accept new data info from %s:%s...' %addr) # 首先向 客户端发送 hello 消息 sock.send(b'Welcome!') while True: data = sock.recv(1024) time.sleep(1) if not data or data.decode('utf-8') == 'exit': break sock.send(('Hello %s' %data.decode('utf-8')).encode('u
1) How to communicate between processes in the networkHow do the processes in the network communicate? The first thing we do is to "uniquely identify a process". Locally, the PID identification process can be used, but it is not possible in the network. The TCP/IP protocol family gives us the answer: the IP address in the network uniquely identifies the host in the network, and the Transport layer's Protocol + port uniquely identifies the application (process) in the host."
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
The following starts to construct the HTTP packet,
The IP layer and the TCP layer use the Python impacket Library, and the HTTP content is filled out by itself.
Copy Code code as follows:
#!/usr/bin/env python
#-------------------------------------------------------------------------------
# Name:raw_http.py
# Purpose:construct a raw HTTP GET packet
#
# Author:yangjun
#
# created:08/0
Socket network programming for python O M development 04Note:This article comes fromFrom Beijing Old Boy linux O M training center-O M development course free video content, The content of this article is a series of content, for more information can be seen: http://oldboy.blog.51cto.com/2561410/1120636
1) Video practices (better full-screen viewing)
For m
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.