introduction
A file transfer between two b.py a.py and procedures: a.py ----------> File ------------> b.py
If the different computer: computer network disk, qq, etc., two programs communicate
Software development framework
Application of communication between the two programs is broadly divided into two types:
A first application categories: qq, micro-channel, network disk, cool this type are to be installed desktop applications
The second is web categories: application Baidu, know almost, blog park accessed using a browser can be used directly
The nature of these applications are in fact the communication between the two programs, which in turn corresponds to the two classification two software development architecture
c / s architecture
client and sever, client and server architecture that is from the user level (can also be a physical level) to division
Here the client generally refers to the client application exe, programs need to be installed after the order on the user's computer, the user's computer operating system environments greater reliance
B / s architecture
browser and sever, Chinese meaning: the browser-side and server-side architecture, which is divided from the user level
Browser browser, the client is actually a client, but the client does not need to install the application, just on the browser via HTTP server related resources (web resources) request, the client browser will be able CRUD Browser check
Networking Basics
How to find a program on another network program: ip address to each computer precise, accurate port to a specific program
IP address is the Internet Protocol address (in English: Internet Protocol Address, also translated as Internet Protocol address) is the IP Address of abbreviations. IP address is a uniform address format of the IP protocol provides, assign it a logical address for each network on the Internet and each host in order to mask the differences in the physical address.
An IP address is a 32-bit binary number, it is generally divided into four "8-bit binary number" (i.e. 4 bytes). IP addresses are typically "dotted decimal" expressed as (ABCD) form, wherein, a, b, c, d are 0 decimal integer between ~ 255. Example: dotted decimal IP address (100.4.5.6), is actually a 32-bit binary number (01100100.00000100.00000101.00000110).
"Port" is the English translation of the port can be considered communication device to communicate with the outside world exports.
osi seven layer model
Nature of the Internet is a series of network protocols, this protocol is called OSI protocol (a series of agreements), according to different functions, different division of labor, hierarchical seven man-made, in fact it does not exist seven, without which seven the concept, just think of dividing it, distinguish purpose is to understand that one is doing
socket concept
Socket is a middleware abstraction layer application layer and the TCP / IP protocol suite to communicate, which is a set of interfaces, in design mode, Socket actually a facade mode, It is responsible for the TCP / IP protocol suite is hidden behind Socket interface for users, a simple interface is all set, let Socket to organize data in order to comply with the specified protocol
Socket is a module, the module is established by the method call connection and communication has been achieved between the two processes, it was also said to be socket ip + port, because the Internet ip is used to identify the location of a host, and is used to represent a port application on this machine, it is only necessary to establish the ip and port will find an application, and to communicate using the socket module
Based on the file type of socket family
Socket Family name: AF_UNIX
unix everything is a file, the file socket calls is based on the underlying file system to capture data, two sockets processes running on the same machine, communication can be done by accessing the same file system indirect
Based on the type of network sockets family
Socket Family name: AF_INET
All addresses family, AF_INET is the most widely used, python support multiple address families, but because we only care about network programming, so most of the time use only AF_INET
tcp and udp protocol agreement
TCP (Transmission Control Protocol) is a reliable, connection-oriented protocol (eg: call), a full duplex communication transmission efficiency is low (buffer transmit & receive cache), facing the byte stream, TCP application using: Web Browser; Electronic e-mail, file transfer program
UDP (User Datagram Protocol) unreliable, unable to connect services, high transmission efficiency (time delay before sending small), one to one, one to many, many-to-many, the face of the message, make the greatest efforts service, no congestion control, application using UDP: domain Name system (DNS); video streaming; IP language (VoIP)
Socket (socket) early use
Socket TCP-based protocol
TCP is a link, it must first start the server, and then start the client to the server-based links
# Sever side (server side) Import Socket SK = socket.socket () sk.bind (( ' 127.0.0.1 ' , 8898)) # binds to a socket address sk.listen () # monitor link conn, addr sk.accept = () # to accept client connections RET = conn.recv (1024) # receiving client information Print (RET) # print client information conn.send (B ' Hi ' ) # send information to the client conn. use Close () # close the client socket sk.close () # close the server socket (optional) # (client) client-side Import socket SK = socket.socket () # create client socket sk.connect (( ' 127.0.0.1 ' , 8898)) # attempt to connect to the server sk.send (b ' the Hello! ' ) RET = sk.recv (1024) # dialogue (send / receive) Print (RET) sk.close () # close the client socket
You may experience problems
# Add a socket configuration, ip and port reuse Import socket from socket Import SOL_SOCKET, the SO_REUSEADDR SK = socket.socket () sk.setsockopt (SOL_SOCKET, the SO_REUSEADDR, . 1) # is that it, before adding the bind sk.bind (( ' 127.0 .0.1 ' , 8898)) # the address bound to the socket sk.listen () # monitor link conn, addr = sk.accept () # accept client link RET = conn.recv (1024) # receiving client information Print (RET) # print client information conn.send (B ' Hi ') # Sending information to the client conn.Close () # close the client socket sk.close () # close the server socket (optional)
Based socket UDP protocol
UDP is a link, you can receive messages directly after starting the service does not need to establish a link in advance
# Sever end (server-side) Import socket udp_sk = socket.socket (of the type = socket.SOCK_DGRAM) # create a server socket udp_sk.bind (( ' 127.0.0.1 ' , 9000)) # bind server socket MSG, addr = udp_sk.recvfrom (1024 ) Print (MSG) udp_sk.sendto (B ' Hi ' , addr) # dialogue (Reception and transmission) udp_sk.close () # client (client) Import Socket for ip_port = ( ' 127.0.0.1 ' , 9000 ) udp_sk.sento (B ' Hello ',ip_port) back_msg,addr = udp_sk.recvfrom(1024) print(back_msg.decode('utf-8'),addr)
# QQ chat Import Socket for ip_port = ( ' 127.0.0.1 ' , 8081) # binds to a socket address udp_server_sock = socket.socket (socket.AF_INET, socket.SOCK_DGRAM) # creating a server socket udp_server_sock.bind ( for ip_port) # bind server socket the while True: qq_msg, addr = udp_server_sock.recvfrom (1024 ) Print ( ' from [% s:% s] of a message: \ 033 [. 1; 44M% S \ 033 [0m ' % (addr [0], addr [. 1], qq_msg.decode ( ' UTF-. 8 ' ))) back_msg = INPUT ( ' reply message: ').strip() udp_server_sock.sendto(back_msg.encode('utf-8'),addr) import socker BUFSIZE = 1024 udp_client_socket = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) qq_name_dic = {'金老板':('127.0.0.1',8081), '哪吒':('127.0.0.1',8081), 'egg':('127.0.0.1',8081), 'Yuan ' :( ' 127.0.0.1 ' , 8081 ),} the while True: qq_name = INPUT ( ' enter chat with: ' ) .strip () the while True: MSG = INPUT ( ' Enter message, sending a carriage return, input q end talking to him: ' ) .strip () IF MSG == ' . 1 ' : BREAK IF Not MSG or Not qq_name or qq_name Not in qq_name_dic: Continue udp_client_socket.sendto (msg.encode ('utf-8'),qq_name_dic[qq_name]) back_msg,addr = udp_client_socket.recvfrom(BUFSIZE) print('来自[%s:%s]的一条消息:\033[1;44m%s\033[0m' %(addr[0],addr[1],back_msg.decode('utf-8'))) udp_client_socket.close()