Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Socket & Endpoint
#1
Hi, everyone. Here is the short version of my question: What is an endpoint instance?

First I want to understand Asynchronous Python, then I find an article in Medium. The author mentioned Eventlet Networking Library, so I went to the docs page. It says:

Quote:Eventlet is a concurrent networking library for Python that allows you to change how you run your code, not how you write it.

Again, I feel confused. What dose a networking library do? I googled 'python networking' and find a tutorial about Python Networking Library.

Quote:Python provides two levels of access to network services. At a low level, you can access the basic socket support in the underlying operating system, which allows you to implement clients and servers for both connection-oriented and connectionless protocols.

So, what is the socket? Google socket again. In Stack Overflow I found an answer:

Quote:A TCP socket is an endpoint instance defined by an IP address and a port in the context of either a particular TCP connection or the listening state.

What is an endpoint instance? I read some articles but I still have no idea. Did I miss something fundamental? Why are there so many concept that I can't understand? Any suggestions? What should I learn now?
Reply
#2
this might be helpful: https://python-forum.io/Thread-Exploring...ad-of-time
Reply
#3
(Sep-13-2017, 12:03 PM)Larz60+ Wrote: this might be helpful: https://python-forum.io/Thread-Exploring...ad-of-time

Thank you, I'll read the post you recommend.
Reply
#4
The internet (and networking) is a bunch of layers of interfaces and apis, each making the next layer slightly easier to work with.  A socket is one of the lowest levels there are. 

It's kind of like building materials.  You can go buy a door... or you can go to a lower level, and buy some wood, some screws, and a couple hinges, and make your own door.  A socket is like the screw in this example.  Most people will just skip it, and use something that's built on top of it.  But if you're learning what they are, it can be neat to see how they work.

Your network interface (some computers have more than one) has an ip address.  That's how other computers can contact you.  An ip address is a lot like a house's address.  The network interface also has ports.  Those are lines of communication that connect programs on the computer, to the outside world.  In the house example, a port would be something like a window or a door.  A single network interface has many, many ports (more than 10,000).

A socket is how a program claims a port for itself, so no other program can use that port.  The process of claiming a port is called "binding".  But that just lets a program listen... other programs need to know which port to try to use to contact your program.  This is called a server.

A different program can open a socket to that same port, for sending.  That's called a client.  Once a client connects with a server over a port, they can begin sending information back and forth to each other.  Either the client, or the server, can close the connection, which is called "breaking the socket".

"endpoint instance" is just another phrase for describing which port you're listening to.

Really, though, you probably don't want to use sockets.  TCP (how webpages work) is very verbose at the socket level.  And by verbose, I mean there's a lot of communication back and forth
"Helo server!" 
"eloh client!" 
"I'd like page xxx/xxx/xxx.html please" 
"sure thing, pal!  it's 2492 bytes long"
"2492 bytes, ok, i'm ready and waiting to start downloading"
"cool, here it comes!  *sends 256 bytes*"
"I just sent 256 bytes, did you get them?"
"I sure did!  Keep sending the rest!"
"ok, here it comes!  *sends 512 bytes*"
"I just sent 512 bytes, did you get them?"
"no, I didn't.  Please try again"

on and on and on.  Using a networking library on top of that makes things so much easier.

...and don't dig too deeply into it.  You might become terrified about how the giant bowl of spaghetti we call the internet is barely held together by yarn, and it's continuing functionality at acceptable speeds is incredibly amazing.
Reply
#5
Everything nilamo says is so true.
But, if you are a masochist, you can get all you ever want to know here: https://www.rfc-editor.org/
Beginning with the first idea April 1969, to the 8,247th entry Sept, 2017
Reply
#6
nilamo, thanks for your reply.

There are so many big words about Computer Science but you explain the concept with some words that I can understand. But still, there's one question. First you said: A socket is how a program claims a port for itself, so no other program can use that port. Then you said: A different program can open a socket to that same port, for sending.

Does it mean that a port can allow only a program to listen on it while other program can only sending requests to it? I wonder if you miss some steps in order to simplify the explanation. Before I received the reply I read an short article: What is a Socket?

Here's the description:

Quote:To make a connection request, the client tries to rendezvous with the server on the server's machine and port. The client also needs to identify itself to the server so it binds to a local port number that it will use during this connection. This is usually assigned by the system. If everything goes well, the server accepts the connection. Upon acceptance, the server gets a new socket bound to the same local port and also has its remote endpoint set to the address and port of the client. It needs a new socket so that it can continue to listen to the original socket for connection requests while tending to the needs of the connected client. On the client side, if the connection is accepted, a socket is successfully created and the client can use the socket to communicate with the server.

My understanding is both the server and the client will have their own new socket which is binding to the same local port number on the client's machine after the server accepts the connection. Am I right?

Oh, I also want to say thanks to Larz60+. Good resource!
Reply
#7
The client and the server will each have their own, separate, independent sockets, which happen to be connected at the same port.  Only one listener can bind to a port, but many clients can bind to it (that's how more than one person can play a multiplayer game in realtime... if only one client could be connected at a time, the only multiplayer games would be games that didn't use the network, or that were turn based).

We're starting to get into a realm where I no longer have any idea what I'm talking about, though, lol.  Because a server frequently will have a pool of connections that's it's waiting on, and it'll send replies to whichever one is ready first.
Reply
#8
If you are really interested in TCP/IP (It's not for the faint at heart),

I suggest purchasing this books by W. Richard Stevens:

TCP/IP Illistrated, Volume I, 2nd edition (The Protocols) (Addison-Wesley Professional Computing Series) New $56.74
TCP/IP Illistrated, Volume II, (The Implementation) New $63.99
TCP/IP Illustrated, Volume III (TCP for Transactions, HTTP, NNTP and the Unix Domain Protocols) New $21.00

It takes a long time to learn, but can be very rewarding (depending on your poinit of view)

Don't expect to get through these in a month, perhaps not even in a year
Reply
#9
Thank you all. Actually I don't want to be an expert on TCP/IP. I realize that I have go far beyond what I really need. After all I just want to understand python async. I googled the keyword but I met more and more concepts that I think I should figure them out. Python async → Eventlet → Python networking → socket → ...

I think I'm on the wrong path. I need to rethink about it. Thank you all!
Reply
#10
I think you might have been misdirected.  async is completely unrelated to networking, so sockets is also unrelated.  Eventlet is a networking library that happens to be written to take heavy advantage of async.
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020