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:
- has a comment (not to be understood at this time)
- 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)
- imports socketserver (sets up server or TCPServer instance, it is the server)
- imports sys module
- imports socket to get ip address of the server for connections over HTTP
- declares an integer to store the port number on which server should listen to requests
- comment
- try statement (can be ignored)
- deploys the server using socketserver.TCPServer() class and passing (address, port) and request handler as arguments to its constructor
- can be ignored (except statement)
- error line (if server is not working)
- exits the program if server isn't working
- this function gets the ip address of the machine
- 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
- returns ip address
- main() function definition
- prints the web address on which the server is available
- outputs to the user letting them know that the server is active
- 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
- 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
Post a Comment