The content of this article: URL, for the later study of HTTP do some foreshadowing

The guide

English: protocol

Protocol /ˈ ʊ ʊtəkɑːl/

Three things Sir Lee invented

WWW = URL + HTTP + HTML

As you’ve seen before in HTML, HTML + CSS is essentially a web page

With URL and HTTP, we have a basic understanding of WWW (URL is a foreshadowing of HTTP).

What is the IP

⭐️ : There is an important connection between the P of HTTP and the P of IP.

IP: The Internet, the whole human network, the protocol.

  • This shows that IP is also very low-level, no IP can not access the Internet

Internet Protocal basically agrees on two things:

  • 1. How to locate a device
  • 2. How to encapsulate data packets to communicate with other devices
    • We don’t care about the details

As long as you are on the Internet, you have at least one unique IP

IP is classified into Intranet and extranet

The Intranet and the Internet cannot be directly connected, but can only be connected through a router as a gateway

Router: a very important device that connects the internal and external networks

For example: if you are playing LOL and you press the Q button (the first skill), how do you let the opponent at the other end of the line get Q?

  • Q is transmitted to the router through the keyboard, and the router is transmitted to the QQ game server through the telecom server. After the QQ game server receives the Q key, it will send the Q key to the opponent, and the opponent will be reached by Q
  • The whole process is very complicated, but very fast. Press the Q key and the response is instant. Because the complicated process is running at the speed of light, the user can’t feel it

How do I obtain an external IP address

You must have an external IP to play LOL, King of Glory…

  1. First, you have to rent bandwidth from telecom, over a thousand a year.

    (Then you need a cat (modem) to convert the signal into something the router can understand. Now it’s common to use light cats, which turn light signals into electrical signals, into network signals that routers can understand.)

  2. You buy a router and use your computer and your phone to connect to WIFl, which the router broadcasts.

    • Wired connections are not intrinsically different from wi-fi, but in practice wireless may be slower than wired connections
    • Download speeds may exceed 10M/s on wired connections, but may not exceed 10M/s on wifi-connected devices
  3. As long as the router is connected to telecom’s server, the router will have an “extranet IP”, such as [14.17.32.211]. This is your address on the Internet.

    • Visit IP138.com/ to check the current Internet IP (usually do not tell others the Internet IP, can accurately locate your location)
    • So all of your devices are going to be this one extranet IP
    • So how to distinguish between mobile devices… A notebook? This depends on the internal IP address
  4. However, if you restart your router, you are likely to be reassigned an “extranet IP”, which means your router does not have a “fixed extranet IP”.

    • If you want to fix the Internet IP, you need to spend five to six thousand yuan per month, you can get a fixed Internet IP (buying these network resources in China is usually more expensive than abroad)
  5. But here’s a question. If your router’s extranet IP is 14.17.32.211, what are the IP addresses of your phone and computer? The answer is Intranet IP.

Network IP

  1. The router creates an Intranet in your home, and the devices on the Intranet use Intranet IP addresses. Generally, this IP address is in the format of 192.168.xxx.xxx.

    • Run the ipconfig command to view the Intranet IP address of the current device: 192.168.101.10

      The DEFAULT gateway IP address: 192.168.101.1 is the IP address of the router

    • If your home is large enough, the router may not be powerful enough to cover the entire home, and users may need to buy a power cat or a small router as an intermediary to extend coverage to the entire home.

    • If the home is 50 or 60 square meters, the router can cover the whole.

  2. Generally, routes assign themselves an easy-to-remember Intranet IP address, such as 192.168.1.1

    • The IP address of the Xiaomi router is usually 192.168.31.1, in order to make room for the IP address of the light cat
  3. The route assigns a different Intranet IP address to each device on the Intranet, for example, 192.168.1.2 for computers, 192.168.1.3 for mobile phones, and so on.

Router functions

You route requests from the internal network to the external, and you route responses from the external network to the internal. It has the function of two-way routing, so it is also called a gateway

  1. The router now has two IP addresses, one external IP (assigned by telecom) and one internal IP (assigned by itself)
  2. Devices on the Intranet can access each other, but cannot directly access the Internet
  3. If an Intranet device wants to access the Internet, it must go through a router
  4. Devices on the extranet can access each other, but not your internal network
  5. If an extranet device wants to send content to the Intranet, it must also go through a router
    • To download content from the Internet (extranet), the content is first downloaded to the router, which then downloads the content to the user’s device
  6. That is to say, the Intranet and the extranet are like two isolated Spaces, unable to communicate with each other, and the only connection is the router
  7. So routers are sometimes called gateways.
    • One man can hold out against ten thousand. Once you unplug the router, you’re disconnected from the Internet

You can create a service with http-Server

  • Command line: http-server. -c-1

  • The first address 192.168.xxx.xxx:8080 is for mobile phone access (it can be used when debugging content on mobile phone is needed).

  • The second address, 127.0.0.1:8080, is for computer access

Several special IP addresses

, respectively,

  1. 127.0.0.1 represents yourself (specified dead)
  2. Localhost is specified as itself through hosts
  3. 0.0.0.0 does not indicate any device

Q: Where is the hosts file?

A: In Windows, hosts is located at C:\Windows\System32\drivers\etc\hosts. On macOS/Linux, hosts is located in /etc/hosts

Hosts file that specifies the local IP (representing yourself)

  • 127.0.0.1 / localhost is the default in hosts (representing itself)
  • You can also add a custom in the hosts fileAny stringThe # in the file is a comment, can be deleted.
    • Written as follows, these strings can be used to access local services once defined
+ 127.0.0.1 sam
+ 127.0.0.1 ryuuks
Copy the code

Note: The following addresses can be used only after the local service is enabled through http-server

http://localhost:8080/index.html
http://127.0.0.1:8080/index.html
http://sam:8080/index.html
http://ryuuks:8080/index.html
Copy the code

Index.html can be accessed from all of the above addresses

port

Now that I have the IP, what else do I need? port

WWW = URL + HTTP + HTML

  • Urls are used to locate servers, for example, Baidu’s baidu.com server

One machine can provide many services

Each service has a number called port number

Port to port

A metaphor

  • McDonald’s offers two Windows, fast food number one and coffee number two
  • When you go to a fast food window and order coffee, you are turned away and sent to another window
  • Same thing when you go to the coffee window and order a snack

In the computer world, too, you need different Windows to provide different services. This window is port

One machine can provide different services

  • Port 80 is best used to provide HTTP services
  • Port 443 is best used to provide HTTPS services
  • Port 21 is the best port to provide FTP service
  • A total of 65535 ports (almost sufficient)

How do I know what port to use

Wikipedia lists them one by one, looking them up when needed: What is each port used for

port describe
80/TCP,UDP Hypertext transfer protocolOr (HTTP)Fast UDP network connection- Used to transfer web pages
For example: Baidu server to transmit a web page to the user, it must use port 80, with other ports will have problems
443/TCP Hypertext Transfer Security protocolOr (HTTPS)QUIC
HTTPS transfers must use port 443, and there are problems with other ports
21/TCP,UDP File transfer protocol(FTP) – Control port
The generation born in the 1980s and the early 1990s should have seen the FTP protocol in school, so they send files through FTP. Millennials probably haven’t seen FTP yet

What are the other rules for port usage

  1. Ports 0 to 1023 (2 ^ 10 minus 1) are reserved for system use

    • Ordinary users must not use it
    • Even administrators who have access to these ports need to enter an additional password to use them, so they are not recommended
    • Port 8080 is usually used for development. When deploying the server, you can use port 80 only if you have the server permission.
  2. You can only use these 1024 ports if you have administrator privileges

  3. Other ports can be used by common users

  4. For example, http-server uses port 8080 by default

  5. If a port is occupied, you have to replace it

Enable services on other ports

  1. Enable the service on port 1234

    Hs-c-1 -p 1234 (hs: http-server)

    http:/ / 192.168.101.10:1234
    http:/ / 127.0.0.1:1234
    Copy the code

    By visiting http://localhost:1234/index.html you can visit the local page

  2. Can I forcibly use port 80 to enable the service? 🉑

    You can use this port only on Windows. You must enter a password to use this port on both Linus and Mac

    Hs-c-1 -p 80 (enable service on port 80)

    http:/ / 192.168.101.10:80
    http:/ / 127.0.0.1:80
    Copy the code

    By visiting http://localhost/index.html you can visit the local pages (80 is the default port, can be omitted)

  3. If the port is occupied, the following error will be reported. Change the port number to another one (more than 60,000 ports).

    hs -c-1 -p 80= >Error: listen EADDRINUSE 0.0. 0. 0:80 // EADDRINUSE: Error address in use The error address is in use
    
    hs -c-1 -p 1234= >Error: listen EADDRINUSE 0.0. 0. 0:1234 // error address in use
    
    hs -c-1 -p 2233
    =>  http:/ / 192.168.101.10:2233
    =>  http:/ / 127.0.0.1:2233
    Copy the code

Both IP and port are indispensable

To sum up: you can’t have both IP and port (remember that)

  • IP is used to locate a device.
  • A port is a service used to locate a device.
  • With IP and port, services provided by a device can be located.
  • For example, the web services provided by Baidu devices. Chat services provided by Tencent devices. Payment services provided by Taobao devices
  • These services can be uniquely identified through IP + port

What is a domain name

A domain name is a friendly name for IP.

The domain name is.com.net.org… A string at the end, that’s all. You have to pay for it

Such as: qq.com

  • When I set up my personal blog, I had registered my domain name.

  • Instead of having my own server, I mapped the domain name to github’s server

  • Github gave me four ips. Users can access any of the four servers. The content is the same

Supplement:

  • .com domain name, relatively cheap, 50-80 a year
  • T.t, used to be the domain name of Smartisan mobile phone, changed hands for 50W. Visit t.t to go to smartisan.com
  • CAI Wensheng, 2000 more than one hundred thousand domain name registration, turnover business. Qiyi.com is the domain name of Baidu’s IQiyi
  • Why do we need to buy domain names from people outside the country? Those who issue domain names also make a lot of money. This is how Americans and Europeans make money. I invent a set of things that all users need to use, and every time they use it, they have to pay.
    • For example, Apple has a lot of phone patents. Google bought MOTOROLA, which also has a lot of phone patents. Google invented Android, and mobile phone manufacturers have to pay Google to use Google services in Android.
    • China’s 5G is coming out. If the world’s 5G applications are led by China and the key technologies are Chinese, Then China can just sit back and make money. If China’s 5G takes off, not just 5G, but all the industries around it will outperform the rest of the world.

In the Internet, every new thing out, contain huge business opportunities. Such as 5 g

A domain name is an alternate name for IP

What IP address does Baidu.com correspond to

  • Command line: ping baidu.com

    220.18138.148.  / / there are 39.156.69.79
    Copy the code

What IP address does QQ.com correspond to

  • Command line: ping qq.com

    125.39.52.26
    Copy the code

knowledge

To sum up: there is no necessarily one-to-one relationship between IP and domain name

  1. A domain name can correspond to different IP addresses (load balancing)

    • This is called load balancing, in case a machine fails
  2. An IP address can correspond to different domain names (shared hosts)

    • For example, six people share a host, which serves six domain names
    • It’s called shared hosting. Poor developers do it
    • Or a company that offers different websites, but doesn’t want to buy two servers, puts two websites (two domain names) on the same host

Ping the same domain name may not result in the same IP address. This involves load balancing.

  • If, ten thousand people visit Baidu at the same time, if only a server, be about to accept ten thousand people at the same time.
  • If you have a million people accessing at the same time, even if only 1K of memory is allocated to each person, you need 1k of memory, and this server can’t hold it
  • The solution is to bind the domain name to four different IP addresses located in central, south, east, and North China.
  • Telecom has a national server, tell telecom, if the user from Central China, access central China server… (1/4 split)
  • This implements load balancing. Spread the load so that a server doesn’t go down because it can’t handle it. (Website optimization Strategy)

Supplement:

  • The Story of The Ping Program

  • Pikachu stands up. It’s the pickup soldier. What is Pikachu when it runs? The answer 😂

How do domain names correspond to IP addresses

Through the DNS

  • Domain Name System (DNS)

When you type baidu.com

process

  1. Your Chrome browser will ask the DNS server provided by China Telecom/Unicom what IP address does Baidu.com correspond to

  2. Telecom/Unicom will answer an IP (the specific process is very complicated, not research)

    • This process can be simulated using nslookup (NS domain name, lookup lookup) command line: nslookup baidu.com

  3. Chrome then sends the request to port 80/443 of the corresponding IP address

  4. Request content is to view baidu.com home page

    • When Baidu receives the request, it will send you the index.html of the page you requested. Then CSS and JS will be requested in index.html. After loading CSS and JS, you can see the complete page of Baidu.com

You can view all requests in network

  • Request URL: www.baidu.com/ (the/at the end indicates the Request is the root directory of Baidu)

  • Response: [get a < HTML >…
    ], the browser will render this string of HTML (link in the middle will request the corresponding image, CSS, JS and other files)

Why port 80 or 443?

Because that’s the rule.

Why don’t we write the port number when entering Baidu.com in the address bar? IP and port are both indispensable!

  • The server provides HTTP services with 80 by default

  • The server uses 443 to provide HTTPS by default

  • You can see the specific ports in the developer tools

    61.135.168.125:443

    Access to HTTP is 80, access to HTTPS is 443

digression

The front-end needs to know some domain name knowledge

www

  • www.baidu.com and baidu.com
  • Is it the same domain name? not

What is their relationship

  • Com is a top-level domain name (com stands for company, org non-profit organization)
  • Baidu.com is the parent of the secondary domain name (commonly known as the primary domain name)
  • www.baidu.com is a level 3 domain name (commonly known as level 2) subdomain name
  • They are father and son
  • Github. IO gives you the subdomain xxx.github. IO for free
    • So github. IO and XXx. github. IO may not be the same company and have nothing to do with each other
  • So you should know that www.baidu.com and Baidu.com may or may not be the same company
  • Is WWW redundant? Yes, very redundant.
    • Early people added it to identify themselves as WWW, but there was no need to add it because all the domain names were already in the WWW itself, and writing WWW prefixes would have done more to save the day

How do I request different pages

To access a different path, request a different page, as follows (note: the path is not suffix)

  • Developer.mozilla.org/zh-CN/docs/…
  • Developer.mozilla.org/zh-CN/docs/…

tool

  • Use the Chrome Developer Tools Network panel to see the difference
    • Only the Request URL is different

Same page, different content

Different query parameters return different contents

  • www.baidu.com/s?wd=hi
  • www.baidu.com/s?wd=hello

Same content, different locations

Anchor points can do that

  • Developer.mozilla.org/zh-CN/docs/…
  • Developer.mozilla.org/zh-CN/docs/…

Pay attention to

  • The anchor appears to have Chinese, but does not support Chinese
  • # reference booksWill become#%E5%8F%82%E8%80%83%E4%B9%A6recoding
  • Anchor points are not visible in the Network panel because they are not passed to the server.
    • The content after the hashtag is eaten by the browser and not uploaded to the server
    • The anchor points are still in the current page, just in a different location, and there is no need to request data again
    • Note: do not concatenate the user’s input parameters after #. This will be eaten by the browser as the anchor content and the server will not be able to get the parameters

The complete content of a URL

Protocol + Domain name or IP address + port number + path + query string + anchor

  • Protocol: INCLUDES HTTP and HTTPS
  • Domain name: string used to represent the name corresponding to the IP address
  • IP: string used to locate a device (4 segments, maximum 255)
  • Port number: To provide a service, there must be a pair of port numbers (there are more than 60,000)
  • Path: is a string, used to determine the user to visit the page
  • Query parameters: is this page, the user to see what specific content, there may be pagination
  • Anchor point: The content has been retrieved and the anchor point is used to locate the specific content that the user wants to view (for positioning within the document)

Add it all up and you have Sir Lee’s invention of the URL.

URL

  • U: Uniform. Internationalized standard

  • R: Resouce resources. Pictures, web pages… Are all resources

  • L: Indicates a Locator.

Ex. :

  • Even if you don’t, you should know the ports
    • The default HTTPS port is 443 and HTTP port is 80

HTTP (Protocol)

The IP protocol is used to represent the address of each device

And HTTP protocol: is used to express how to transfer web pages

  • HTTP is based on TCP and IP
  • There was TCP and IP, and Sir Lee invented HTTP based on those two protocols

The curl command

  • The curl/k ɜ ː rl/make… curly

Do you want to curl an HTTP request?

Using curl you can send HTTP requests

Example 1:

curl https://www.baidu.com
Copy the code

  • Execute the command to retrieve the entire document. (Chinese default UTF8 encoding, and console default GBK so Chinese will be garbled)
  • We type the url into Chrome’s address bar, and Chrome does the same thing, retrieving the HTML document and parsing it

Example 2:

Curl -v http://baidu.com Only domain name without pathCopy the code
  • -v: latent/latent/latent (effect: displays more content)

Requested content

  1. If only the domain name does not have a path in the HTTP request, the root path is requested by default and the slash/of the root path is automatically completed

  2. Enable the agent, using some of the agent’s functions (optional)

  3. Asked DNS: Obtained the IP address of Baidu.com

  4. After obtaining the IP address, TCP is started

  5. Start connecting to port 80 of Baidu.com (IP address 220.181.38.148)

  6. The connection was successful and the 5 lines of content were sent to Baidu.com

    1. GET request, request/root path, using HTTP version 1.1
    2. An IP address (220.181.38.148) may correspond to multiple domain names. Therefore, you need to access the Baidu.com domain name of this IP address
    3. Do you want to use curl version 7.61.1 to access curl
    4. What do I expect to return: */* means I accept everything
    5. A blank line (sending a carriage return) indicates the end of the request header

What is returned

  • Response header: ignore 😂 first

  • < HTML > < meta… >
    meta, meaning please revisit www.baidu.com

    • Note The baidu.com URL is deprecated. Only the original URL www.baidu.com is used
    • Usually, the people who directly use Baidu.com to visit Baidu are basically programmers, directing the traffic of programmers to WWW

Request curl -v http://www.baidu.com

Example 3:

curl -s -v -- https://www.baidu.com
Copy the code
  • More detail than shown above

The following processes need to be understood

  • The url will be rewritten by the curl tool, which will request DNS to obtain the IP address
  • The TCP connection is established first. After the TCP connection is successful, HTTP requests are sent
  • Request a look at the content
  • Take a look at the response content
  • When the response is complete, close the TCP connection.
  • Truly over (only truly over when TCP is over)

HTTP

What does HTTP do

  • What is the format of the request and the format of the response

HTTP specifies how requests should be written: How should line 5 of step 6 be written

HTTP specifies how the response should be written: How should the following response be written

So how does HTTP specify request and response formats? Next time decompose 👋