Python Socket library to create a server and client scripts

  • #1
fog37
1,549
107
TL;DR Summary
Python Socket library to create server and clients
Hello,

I have been experimenting with the socket library in python creating small scripts, one called server.py and the other client.py, that can communicate with each other using a TCP protocol. The server script has an infinite loop that listens for request from the client.py script and responds to any requests. Both the "server" and the "client" are on the same physical machine, i.e. my computer. To make the communication possible, both server need to have the combination IP+port which represents a socket.

a) I know that name localhost represents an alias the local machine itself and corresponds to the IP 127.0.0.1. However, in my Python scripts, the IP used is the locally assigned IP (169....) which is different from 127.0.0.1....That said, when is 127.0.0.1 used? What is the purpose of it? Why isn't localhost the same as the assigned local IP address?

b) I always thought that for a program to be considered a "server", the program needed to be a sophisticated program capable of addressing multiple requests from multiple computers/clients at the same time and provide some service. But even a regular printer has a server program running on it, correct?

c) And some application seem to have a server/client structure in the sense that the application software is in the form of a server program. For example, MySQL is "server application" and the interface to it is the client. I wonder why some applications choose that architecture. Microsoft Word, Excel, PowerPoint, Adobe Acrobat, programs like Python, are not server programs. They are applications that when run also provide a user interface....

d) It is surely possible to send information from a computer to another connecting the computers with an ethernet cable as I have done in the past. That exchange of information does not involve any server though...Why? What is the difference then? Why do we need a server program?

thank you!
 
Technology news on Phys.org
  • #2
@fog37 it is very difficult to give any useful feedback on a computer program without seeing the source code.
 
  • #3
fog37 said:
I always thought that for a program to be considered a "server", the program needed to be a sophisticated program capable of addressing multiple requests from multiple computers/clients at the same time and provide some service.
That is one meaning of "server", because in the real world (meaning, outside of toy programs that we write to learn some aspect of programming--when I was learning how Python does network programs, I wrote toy programs much like the ones you're writing) the things we call "servers" are sophisticated programs that have the features you describe.

But in the context of simple programs like the one you're writing, the only real difference between "client" and "server" is that the "server" is the program that listens for a connection request, and the "client" is the program that makes the connection request. Once the two programs are connected, they can just talk to each other and it doesn't really matter which is the "client" and which is the "server".

fog37 said:
even a regular printer has a server program running on it, correct?
Yes, and its job is to listen for connection requests from computers that want to print something, and then handle those requests as they come in. (I'm assuming you are talking about a networked printer, as opposed to one that is connected to a single computer--the latter type of printer might not have any server running on it at all, it depends on the single computer it is connected to to just tell it what to print, with no connection process of any kind required.)

fog37 said:
I wonder why some applications choose that architecture.
Because it's been found to be the best architecture for those kinds of programs. For example, MySQL is a database. Its job is to store data and respond to requests to read data, write data, query data, etc., etc. So it makes sense for it to be a server with clients that talk to it.

Microsoft Word, by contrast, is a program for people to use to write documents. There is no good reason why it needs to be a server or have a client-server architecture, so it doesn't. (But contrast that with, for example, Google Docs, which can also be used to edit Word documents, but provides networked collaboration functions that Microsoft Word does not. And Google Docs does run on a server, with clients that talk to it.)

fog37 said:
It is surely possible to send information from a computer to another connecting the computers with an ethernet cable as I have done in the past. That exchange of information does not involve any server though
That depends on what protocol you are using. TCP is designed as a client-server protocol. Other protocols are not: for example, UDP has no distinction between "client" and "server"; if two programs on different computers on the same network each know what IP address and port number the other is using, they can communicate using UDP datagrams without ever having to make a connection and without either one being designated as the "client" or the "server".
 
  • Like
Likes fog37
  • #4
PeterDonis said:
That is one meaning of "server", because in the real world (meaning, outside of toy programs that we write to learn some aspect of programming--when I was learning how Python does network programs, I wrote toy programs much like the ones you're writing) the things we call "servers" are sophisticated programs that have the features you describe.

But in the context of simple programs like the one you're writing, the only real difference between "client" and "server" is that the "server" is the program that listens for a connection request, and the "client" is the program that makes the connection request. Once the two programs are connected, they can just talk to each other and it doesn't really matter which is the "client" and which is the "server".


Yes, and its job is to listen for connection requests from computers that want to print something, and then handle those requests as they come in. (I'm assuming you are talking about a networked printer, as opposed to one that is connected to a single computer--the latter type of printer might not have any server running on it at all, it depends on the single computer it is connected to to just tell it what to print, with no connection process of any kind required.)


Because it's been found to be the best architecture for those kinds of programs. For example, MySQL is a database. Its job is to store data and respond to requests to read data, write data, query data, etc., etc. So it makes sense for it to be a server with clients that talk to it.

Microsoft Word, by contrast, is a program for people to use to write documents. There is no good reason why it needs to be a server or have a client-server architecture, so it doesn't. (But contrast that with, for example, Google Docs, which can also be used to edit Word documents, but provides networked collaboration functions that Microsoft Word does not. And Google Docs does run on a server, with clients that talk to it.)


That depends on what protocol you are using. TCP is designed as a client-server protocol. Other protocols are not: for example, UDP has no distinction between "client" and "server"; if two programs on different computers on the same network each know what IP address and port number the other is using, they can communicate using UDP datagrams without ever having to make a connection and without either one being designated as the "client" or the "server".
Thank you. All clear. In regards to the printer example, I am thinking of a simple home printer that users can connect to, one at a time, to print document via wifi...Would the software on that printer qualify as a server? Or, as you mention, the printer communicates with hosts requesting printing service via a protocol not designed as a client-server?

It seems like when a piece of software is supposed to be used by more than one user, the client-server architecture is would be the way to go. I read that Windows 10/11, which is an operating system, is to be considered a "File server" and offers many other network services...

A while back we talked about how Jupyter notebook works and you truly help a lot in that discussion (always do). Jupyter notebook is a prime example of client-server application: the client is the browser, there is a local web server, and there is the ipython kernel (which I envision like a python interpreter with more wrapping around and whose job is to process the python code in the notebook cells).

Jupyter notebook is not necessarily designed to be used by multiple users...In my case, I have a machine and I am the sole user...

Back to the mechanics, the Jupyter server talks to the browser (using web protocols TCP/IP) and also talks to the Ipython kernel (using a ZMTP protocol). The Ipython kernel (which existed before Jupyter) is really like a "specialized python interpreter" that can receive a jupyter notebook (a json file with markdown, graphs, etc.), process the Python code inside its cells, send the results back the sever which sends it to the browser...The browser URL shows localhost:8888 meaning that the browser is a process at port 8888...Or are those the IP address and port of the jupyter server? The localhost is the alias for 127.0.0.1...My computer has a different local IP though. Shouldn't all process share the same IP, since they are on the same machine, but different ports?
1708173198472.png


Another more detailed diagram of what is happening under the hood:
I see the yellow box with the different possible kernels (depends on the programming language we use). The blue box comprises different possible server options...What do the purple box and green box represent in the relation to the browser (client)?

1708173739940.png
 
  • #5
fog37 said:
Why isn't localhost the same as the assigned local IP address?
A computer can have several physical or virtual network adapter/interfaces, and each has their own IP address.
On a Windows host you can run ipconfig in a cmd window to see what interfaces you have and their assigned address; on Linux the command is ifconfig.

The loopback (virtual) interface is, as you know 127.0.0.1, and allows for two programs on the same host to communicate directly. Other physical/VPN network interfaces are usually configured to be assigned an IP address based on some communication with an external host, e.g. the interface connected to your local wifi/ethernet will likely use the DHCP protocol to establish this.

When given a IP packet to send, the local host uses its IP routing table to determine which interface to route the packet to, which is a process repeated until the packet reach its destination (or is discarded). On windows you can see this table using route print in a cmd window.

There are of course a huge load of technical details on this depending on which part of the full IP protocol stack you want to dig into.
 
  • #6
fog37 said:
In regards to the printer example, I am thinking of a simple home printer that users can connect to, one at a time, to print document via wifi...Would the software on that printer qualify as a server?
Yes.

fog37 said:
Or, as you mention, the printer communicates with hosts requesting printing service via a protocol not designed as a client-server?
Not for a wifi printer, no.

fog37 said:
It seems like when a piece of software is supposed to be used by more than one user, the client-server architecture is would be the way to go.
Often that is the case, yes.

fog37 said:
I read that Windows 10/11, which is an operating system, is to be considered a "File server" and offers many other network services...
An operating system is not a server. When Windows says it is a "file server", it just means that it manages the file system for programs that run on Windows. Similarly for other "network services".

You could have a computer on a network that was a "file server" in the sense that other computers, as clients, would connect to it and ask for files. But that kind of "file server" doesn't have to run Windows, nor does it have to have any users that use it directly. It might not even have a keyboard, mouse, or monitor.
 
  • Like
Likes fog37
  • #7
fog37 said:
The browser URL shows localhost:8888 meaning that the browser is a process at port 8888...
No.

fog37 said:
Or are those the IP address and port of the jupyter server?
Yes. The browser never tells you directly what port or IP address it is using, only the port and IP address of the server it is connecting to.

fog37 said:
The localhost is the alias for 127.0.0.1...My computer has a different local IP though.
By "local IP" I assume you mean an IP address on a local network? For example, the computer I am writing this on has the IP address 192.168.1.156 on my home's local network.

fog37 said:
Shouldn't all process share the same IP, since they are on the same machine, but different ports?
Not at all. It is common for the same computer to have multiple IP addresses, and for different network programs on that computer to be bound to different IP addresses, depending on what they are doing. For example, your Jupyter notebook browser process is bound to the localhost IP address because all the pieces of the Jupyter notebook application are running on your local computer. But another browser process on your computer could be bound to a local network IP address or even a public IP address (depending on where you are posting from and whether your computer is behind a router that does Network Address Translation) in order to talk to the Physics Forums server so you can post here.
 
  • #8
PeterDonis said:
Yes.


Not for a wifi printer, no.


Often that is the case, yes.


An operating system is not a server. When Windows says it is a "file server", it just means that it manages the file system for programs that run on Windows. Similarly for other "network services".

You could have a computer on a network that was a "file server" in the sense that other computers, as clients, would connect to it and ask for files. But that kind of "file server" doesn't have to run Windows, nor does it have to have any users that use it directly. It might not even have a keyboard, mouse, or monitor.
Yes, an OS is an OS even if sometimes I hear and read about "server operating systems"...For example, (https://phoenixnap.com/kb/server-operating-system) states that "... a server operating system is an advanced operating system designed to run on servers...."

But all they mean is a machine, with a certain OS installed, which hosts various servers, like a web server, mail server, etc.. For example, Apache is a web server...

Another website mentions that "A server operating system (server OS) runs on a server in a client-server architecture and provides multiple services to client machines within the network. It forms the software backbone to run various programs and applications. A server OS offers advanced capabilities to run, manage, monitor, and control applications, processes, and client devices such as different servers, including a web server, file server, application server, mail server, database server, and more..."
 

Similar threads

  • Programming and Computer Science
Replies
28
Views
475
  • Programming and Computer Science
Replies
17
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
27
Views
3K
  • Programming and Computer Science
2
Replies
39
Views
5K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
15
Views
1K
  • Computing and Technology
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
Back
Top