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 of the computer and talk across the Internet, to talk on the web. 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 a short summary of something I go into in much more detail there. So, one of the things that 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 represents two computers, and there's application, talks to a transport layer, which talks to an Internet layer, which talks to the link layer and that talks to the Wi-Fi or Ethernet connection. It goes through a bunch of hops on the network, maybe 10, 12, 15, and it comes at the end of the destination could be that maybe this is a web server or something. It goes up through these layers and then runs a web application and then the data is then sent back, out the same path back up 15 hops, up and then back into your application and then to your screen. So, you can see something. We can talk a lot about this in the book, and then in my other Coursera class, I talk a lot about these layers and give you all this stuff, but we're going to 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. 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. Another server and gets 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 it really is to 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, there are two pieces of software running on two independent computers that are actually chatting, that's 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. It's little thing that's built in the Internet just long enough to have this conversation happening then it's thrown away. So, we call this thing a socket. The way sockets work is, if you want to establish a socket you say, "I would like to talk to this particular web server." All the web servers have names, and numbers, and points. Then there's also which application. So, the different applications on that server are listening on what are called ports. You could 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 TCPIP 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. 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 within this class the most is port 80. So, port 80 is the web port because that's the part that's connected to the server. Port 443 is the secure HTTPS. 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 hostname or number and then you connect to port 80 and then hopefully if there is 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 is making a phone call, an application on this computer is picking the phone call up, and you both say "Hello". So, in the common TCP ports like I said, port 80 is the one that we're going to play with the most it's HTTP. 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 colon 8080 on it. Well, this basically is the syntax for URLs host and then colon 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. 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's 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 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 a thing across the internet and we're going to use a stream. A stream means it's a series of characters that just keep coming back. So, that's that second line. 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 passed 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. So that's all it takes to make the connection that dials the phone in Python like I said. Amazingly, just three lines. That's one of the things that we love about Python. We absolutely loved 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 the sockets, how we actually communicate to that far off application in Python.