Python Journey. Chapter 8 Network Programming/Operating Systems

1. Socket communication based on TCP protocol (no concurrent effect)

#blocking : the program stopped

Server:

import socket

 

server = socket.socket (socket.AF_INET, socket.SOCK_STREAM)

server.bind(('127.0.0.1',8080))

server.listen(5) # listen to receive connection requests from clients

 

while True:

    conn,client_addr=server.accept()

    print(client_addr)

 

    while True:

        try:

            data=conn.recv(1024)

            if not data:break #For linux system

            conn.send(data.upper())

        except ConnectionResetError: #Linux system will not report ConnectionResetError ( windows system) when unilaterally closing the connection , it will receive empty

            break

    conn.close()

 

server.close()

Client:

import socket

 

client = socket.socket (socket.AF_INET, socket.SOCK_STREAM)

client.connect(('127.0.0.1',8080))

 

while True:

    msg=input('>>>: ').strip()

    if not msg:continue

    client.send(msg.encode('utf-8'))

    data=client.recv(1024)

    print(data.decode('utf-8'))

 

client.close()

 

2. Sockets based on UDP protocol communication

TCP (Streaming Protocol): High reliability. Packets will be lost due to network reasons, but the information will be deleted in memory only when one end receives the ack confirmation message from the other end. There will be a sticky packet problem in TCP , and receiving and sending may not correspond. The server must be started first, otherwise an error will be reported.

UDP (Packet Protocol): Low reliability. Information is deleted in memory once sent, and if packet loss occurs, the information is lost. The main reasons for the high efficiency and speed of UDP are that one is not establishing a connection, and the other is that it will not be confirmed after acceptance. UDP does not have the problem of sticking packets, and there is a one-to-one correspondence between receiving and sending. If you send hello , but receive a character, an error will be reported in the windows system; no error will be reported in the linunx system, only h is received .

Parallel: (in the true sense) run concurrently; parallelism is concurrency

Concurrency: It seems to be running at the same time

 

服务端:

import socket

 

server=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # 数据报协议

server.bind(('127.0.0.1',8080))

 

while True:

client_data,client_addr=server.recvfrom(1024)

#保持UDP稳定(丢包少)的最大接受字节为512,所以这里一般设为1024

    print(client_data)

    msg=input('回复%s:%s>>>:' %(client_addr[0],client_addr[1]))

server.sendto(msg.encode('utf-8'),client_addr)

 

客户端:

import socket

 

client=socket.socket(socket.AF_INET,socket.SOCK_DGRAM) # 数据报协议

 

while True:

    msg=input('>>>: ').strip()

    client.sendto(msg.encode('utf-8'),('127.0.0.1',8080))

    res,server_addr=client.recvfrom(1024)

    print(res.decode('utf-8'))

#因为UDP不需提前建连接,不用等到一个客户端退出后另一个才能交互;几个客户端都可以和服务端交互。服务端还是一个个服务,但因为处理速度快,会给用户一种并发的感觉。

 

URL地址: http://www.cnblogs.com/linhaifeng/articles/6817679.html

http: #浏览器(客户端套接字)使用的协议

www.cnblogs.com             #域名

www.cnblogs.com80         #省略:8080为端口,一般服务端端口默认为80

linhaifeng/articles/6817679.html  #路径

 

DHCP服务器: 每台电脑自带的,用来配IP,其端口默认67

DNS服务器: 每台电脑自带的, 用来将域名转成IP,其默认端口53

 

用浏览器访问某个URL,本质是连接服务器,将其上的文件打开并下载到本地

a   浏览器客户端套接字 –– UDP –– DNS服务端套接字,将域名解析成IP返回

b   浏览器客户端套接字 –– TCP –– Web服务端套接字,将文件下载到本地

常用服务器软件:nginxhttpd

 

DNS查询为迭代查询

一般会现在本地DNS查找,如果找不到会在同级DNS查找,若还找不到需要从根DNS服务器逐级问下来(迭代查询);后续解释本地DNS代劳。

www.cnblogs.com.

.      #跟域名       #13个跟服务器(美913),受限于UDP稳定性

com   #顶级域名

cnblogs  #二级域名    #二级域名需去顶级域名中买

www    #二级域名的主机名

 

三、进程

#串行: 一个一个的运行

1、什么是程序?什么是进程?

    程序说白了就是一堆文件

    进程就是一个正在执行的过程/程序,所以说进程是一个抽象的概念。这个概念起源操作系统

 

2、什么是操作系统

    定义:操作系统是位于计算机硬件与应用软件之间,用于协调、管理、控制计算机硬件与软件的资源的一种控制程序

 

3、操作系统的两大作用:

    1、把复杂丑陋的硬件操作都封装成美丽的接口,提供给应用程序使用

    2、把进程对硬件的竞争变的有序

 

4、多道技术(单个CPU在多个程序中切换以实现并发)

    多道的产生背景是想要在单个cpu的情况下实现多个进程并发执行的效果

    a、空间上的复用 (多道程序复用内存的空间)

    b、时间上的复用  (多道程序复用CPU时间)

    

    cpu遇到IO操作要切换(提升效率)

    一个进程占用cpu时间过长也切(降低效率)

进程与进程之间的内存空间是互相隔离的

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324739861&siteId=291194637