GithubHelp home page GithubHelp logo

breezy-codes / server-client-program Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 9 KB

This Python-based server-client application allows users to query a predefined animal dictionary using UDP socket communication. The server listens for requests and responds with animal names and details, while the client provides an interactive interface for sending queries and displaying results.

Home Page: https://youtu.be/S4oAhXF7hcM

Python 100.00%
python pythonprogram tutorial clientprogram serverprogram

server-client-program's Introduction

Server-Client Animal Database Tutorial

In this tutorial, we will be building a simple server-client Python program that allows the client to send a request for an animal's name, and the server responds with the names of animals of that type from a predefined dictionary.

Prerequisites

  • Basic knowledge of Python programming.
  • Understanding of sockets and basic networking concepts.

Getting Started

Server Side

  1. Open the provided server.py file.

  2. You will notice that we import the socket module. This module provides us with the necessary functions for creating and managing sockets.

  3. Define the IP address and port number on which the server will listen for incoming requests. Update the server_ip and server_port variables with the appropriate values.

  4. Create a socket for communication using socket.socket(socket.AF_INET, socket.SOCK_DGRAM). Here, AF_INET indicates the use of IPv4 and SOCK_DGRAM indicates that we are using a UDP socket for communication.

  5. Bind the socket to the server IP and port using server_socket.bind((server_ip, server_port)).

  6. A while loop has been implemented to keep the server running indefinitely. Inside the loop:

    • Receive data and client address using data, client_address = server_socket.recvfrom(1024).
    • Convert the received data (animal name) to lowercase and strip any whitespace.
    • Check if the received animal name exists in the our_animals dictionary. If it exists, prepare the response by joining the animal names and send it back to the client using server_socket.sendto(response.encode(), client_address). If the animal name is not found, send an appropriate error message.
  7. Close the server socket using server_socket.close().

Client Side

  1. Open the provided client.py file.

  2. Similar to the server side, import the socket module.

  3. Define the server IP and port number in the server_ip and server_port variables.

  4. Create a socket for communication using socket.socket(socket.AF_INET, socket.SOCK_DGRAM).

  5. Enter a while True loop to keep the client running until the user decides to exit.

  6. Inside the loop, prompt the user to enter an animal name using input(). Send the entered animal name to the server using client_socket.sendto(animal.encode(), (server_ip, server_port)).

  7. Receive the response from the server using response, _ = client_socket.recvfrom(1024) and print the names of animals associated with the provided animal type.

  8. Ask the user if they want to continue. If the input is not 'y', break out of the loop.

  9. Close the client socket using client_socket.close().

Usage

  1. Run the server.py file to start the animal database server.

  2. Run the client.py file to start the client.

  3. The client will prompt you to enter an animal type. For example, you can enter "lion".

  4. The server will respond with the names and ages of lions from the predefined our_animals dictionary.

  5. The client will then ask if you want to continue. If you enter 'y', you can request information for another animal. If you enter anything else, the client will exit.

Conclusion

In this tutorial, we've created a basic server-client program to fetch animal names from a server's predefined dictionary. This example serves as a simple introduction to socket programming in Python and can be expanded upon for more complex applications. Remember to consider error handling, security, and scalability when working with real-world networking scenarios.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.