12.1 - Networked Technology

دوره: Using Python to Access Web Data / فصل: Networks and Sockets (Chapter 12) / درس 1

12.1 - Networked Technology

توضیح مختصر

But when you hook these two things together they are two pieces of software running on two independent computers that are actually chatting. Let's say you want to use millions of lines of code in the link and network and transport layer of your computer as well as the entire Internet and some server on the other side and the data and you want to talk to it. So the next thing we're going to talk about is once we've made these sockets, how we actually communicate to that far-off application in Python.

  • زمان مطالعه 0 دقیقه
  • سطح خیلی سخت

دانلود اپلیکیشن «زوم»

این درس را می‌توانید به بهترین شکل و با امکانات عالی در اپلیکیشن «زوم» بخوانید

دانلود اپلیکیشن «زوم»

فایل ویدیویی

برای دسترسی به این محتوا بایستی اپلیکیشن زبانشناس را نصب کنید.

متن انگلیسی درس

Welcome to Chapter 12. Now what we’re going to do is we’re going to take and write programs instead of just talking to a disk drive, we’re going to go right outside the computer and talk across the Internet, to talk on the web. And the Internet, of course, is an exciting thing, and there’s a lot to it, and we’ll talk a little bit about Internet architecture, just enough to motivate what we’re going to do in Python programs. But if you really get excited about this, and find this interesting, I wrote a whole book, it’s a free book, you can also buy it on Amazon, if you like, but just go there. There’s actually lectures, and quizzes and everything, for this free book on network architecture that I wrote, called Introduction to Networking. So this is really short summary of something I go into in much more detail there. So one of the things we talk about in that book, is this thing called the network architecture. There is this layered architecture that basically represents the connection over the Internet. And it’s representing two computers. And there’s an application, talks to a transport layer, which talks to an Internet layer, which talks to a link layer, and that talks to like the Wi-Fi or Ethernet connection. And it goes through a bunch of hops on the network, maybe 10, 12, 15. And it comes into the destination computer, maybe this is a web server or something. And it goes up through these layers, and then runs the web application. And then the data is then sent back, out this same path, back up 15 hops, hop hop, hop, and then back into your application. And then sort it to your screen, so you can see something. And we can talk a lot about this and in the book and in my other Coursera class, I talk a lot about these layers, and give you all the stuff. But we’re going to sort of simplify and skip past all of that stuff. It’s fun, but we’re not going to take too much time on it. But instead, we’re going to start at the first layer that is what’s called an end-to-end layer in the network architecture called the transport layer. And the idea is as you can write a piece of code, you will write a Python program that will talk out the transport layer over to an application on another server, this is the network over here, on another server and get that data back. And that’s what we’re going to show you how to do. So the internet layer and the link layer implement the transport layer. But for now we’re really going to just pretend that there is a nice pipe from end to end that when your program says something down the pipe, the program on the other end hears it. And when that program sends something back you can hear it on your end. It’s a very simple thing. So we call these communications between two applications because there really is two applications. Your Python program on one end and a network server, it might be written in PHP or Java or even Python. But when you hook these two things together they are two pieces of software running on two independent computers that are actually chatting. It is like they made a phone call, you make a phone call, chat on the phone call, hang the phone call up. The difference is that computers do this hundreds of times a second if not thousands of times a second and we call this connection a socket, right? It’s this sort of like little thing that’s built in the Internet just long enough to have this conversation happen and then it’s thrown away, and so we call this thing a socket. And the way sockets work is if you want to establish a socket you say I would like to talk to this particular web server. And all the web servers have names and numbers, end points. And then there’s also which application, so the different applications on that server are listening on what are called ports. You can think of them almost as telephone number extensions. So you call a large company, they’ve got one phone number, and it says enter the extension if you know it. Well that means you’ve called the company but you really want to talk to a person. So these ports, these TCP/IP ports, these little ports, are different applications associate themselves with these ports when they start up. So the incoming email server might be on port 25. And it wakes up and it sits there waiting for something to happen. And so our applications say, you know what? I want to send some mail and so I’m going to connect to this server but on port 25 and that way I hope to talk to the email server. So we use these ports. Now the port that we’re going to play with in this class the most is port 80. So port 80 is the web port, because that’s the port that’s connected the server, port 443 is the secure HTTPS. And so when you connect to a web server, so if you’re a browser and you want to connect to a web server, you go to a host name or a number, and then you connect to port 80. And then hopefully if there’s a web server on that host, you’ll be talking to the web server on port 80. Again, it’s like an application on your computer’s making a phone call and an application on this computer is picking the phone call up and you both say, hello. So on the common TCP ports, like I said, port 80 is the one we’re going to play with the most, it’s HTTP. And so sometimes you’ll even go on the web and cruise around, and see a URL that is a little different, that has something like a :8080 on it. Well this basically is the syntax for URL’s host and then :8080 is the port. So this is basically a web server that’s not running on port 80 but running on port 8080. So it’s not like they have to be on these ports. But we commonly expect them to be on ports. And normally there’s going to be a web server on port 80. If there is a web server. There might not be a web server on port 80. So, Python is awesome. Let’s say you want to use millions of lines of code in the link and network and transport layer of your computer as well as the entire Internet and some server on the other side and the data and you want to talk to it. It takes three lines of Python, three lines of Python, that is it. All that complexity, all that whatever? Three lines of Python. So, like always, we have to first import something, so we’re going to import the socket library to say, hey, we’re going to use this library. Like the regular expression, we said import re. And then we create a socket, we say socket.socket, this syntax here, I don’t want to explain it, just kind of type this, the way I tell you to. It’s like, it creates this endpoint that’s inside your computer that’s ready to connect out to the far end but it’s not yet connected. It’s like a connection point that has not yet been connected, and that’s what this magic means. We’re going to hook our thing across the Internet. And we’re going to use a stream. A stream means it’s a series of characters that’ll just keep coming back. So, that’s that second line, and the actual connection happens when we say, mysock.connect. So, this returns us an object, and then we actually call the connect method in that object. And we pass in two things. We pass the host we want to connect to, which is a domain name, and a port that we want to connect to. So that makes the connection and that’s all it takes to make a socket connection. Now that’s not sending any data. That’s like dialing the phone, okay? So that’s all it takes to make the connection. That dials the phone in Python, like I said. Amazingly, just three lines. And that’s one of the things that we love about Python. We absolutely love the fact that Python does so much for us with a simple import statement. In this xkcd comic, we see someone who imported anti-gravity, and they started floating. So, you get the idea that import socket is like amazing. You can do something in two lines after that and you’re doing something useful. So the next thing we’re going to talk about is once we’ve made these sockets, how we actually communicate to that far-off application in Python.

مشارکت کنندگان در این صفحه

تا کنون فردی در بازسازی این صفحه مشارکت نداشته است.

🖊 شما نیز می‌توانید برای مشارکت در ترجمه‌ی این صفحه یا اصلاح متن انگلیسی، به این لینک مراجعه بفرمایید.