Python3 tutorial - 12 (intro to modules and server setup)

Welcome to CCP
Lets make a server ! (Temporary)

So, before we get started, we should learn how to import external modules and some in-built libraries into our code,

Note: You should have some knowledge of some common CLI (Command Line Interface) commands

lets do it!

For you to understand, lets practice how to import modules


Note: these are all MacOs terminal commands, if you are using windows, these maybe different

also, nano is a widely used Command Line editor which doesn't has much GUI but is powerful,


There's not much in the main() function,

Note: these types of modules or scripts when you want to import, should not contain any print() statements, else,
whenever you will try to import them, they'll run automatically and can produce unwanted outputs

thats why, in this screenshot i haven't used any such statements which produces any output,
if you even try to run a similar script, it'll not produce any output as it only contains definitions for the function and not any calls,


now lets create a new script which will import the module.py script to use main() function in it,


We can refer to main() function of module.py using Dot Notation and import module.py using import module statement

Now lets try to run it and see what do we get:


We use to shell command python3 to run our desired script from the command line interface,

And Yess!! it works, Now its really time to build some real stuff right ?

Hmm...

Lets get started !

You can obtain all the code from my github repo Here,
after clicking the link, go to clone or download and click download zip, the files will be downloaded as zip, just extract them and run server.py using the above command,
you have a running server !

Now lets understand line by line:

  1. has a comment (not to be understood at this time)
  2. imports http.server (important to make a request handler which handles requests and responds to them via HTTP, main role to play for your server)
  3. imports socketserver (sets up server or TCPServer instance, it is the server)
  4. imports sys module
  5. imports socket to get ip address of the server for connections over HTTP

  6. declares an integer to store the port number on which server should listen to requests
  7. comment
  8. try statement (can be ignored)
  9. deploys the server using socketserver.TCPServer() class and passing (address, port) and request handler as arguments to its constructor
  10. can be ignored (except statement)
  11. error line (if server is not working)
  12. exits the program if server isn't working

  13. this function gets the ip address of the machine
  14. socket.gethostbyname() function gets the ip using hostname of the machine and takes the hostname as its argument, and we are giving socket.gethostname() function as its argument as this function gets the hostname and then the fetched ip is stored in the variable
  15. returns ip address
  16. main() function definition
  17. prints the web address on which the server is available
  18. outputs to the user letting them know that the server is active
  19. the TCP server object we declared before tcp_server = TCPServer(...) has a method called serve_forever() which runs the server until the program shuts down or we press ctrl-c to terminate the program and from then on the server does its work
  20. Now you have to ignore the below lines and just call the main() function, Congratulations your server now should run

How's that ?
Let me know in the comment section below !

see you later

Comments