Simple Socket
In the following code, the server sends the current time string to the client:
# server.py import socket import time # create a socket object serversocket = socket.socket( socket.AF_INET, socket.SOCK_STREAM) # get local machine name host = socket.gethostname() port = 9999 # bind to the port serversocket.bind((host, port)) # queue up to 5 requests serversocket.listen(5) while True: # establish a connection clientsocket,addr = serversocket.accept() print("Got a connection from %s" % str(addr)) currentTime = time.ctime(time.time()) + "\r\n" clientsocket.send(currentTime.encode('ascii')) clientsocket.close()
Here is the summary of the key functions from socket - Low-level networking interface:
Note that the server socket doesn't receive any data. It just produces client sockets. Each clientsocket is created in response to some other client socket doing a connect() to the host and port we're bound to. As soon as we've created that clientsocket, we go back to listening for more connections.
# client.py import socket # create a socket object s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) # get local machine name host = socket.gethostname() port = 9999 # connection to hostname on the port. s.connect((host, port)) # Receive no more than 1024 bytes tm = s.recv(1024) s.close() print("The time got from the server is %s" % tm.decode('ascii'))
The output from the run should look like this:
$ python server.py & Got a connection from ('127.0.0.1', 54597) $ python client.py The time got from the server is Wed Jan 29 19:14:15 2014
"If you need fast IPC between two processes on one machine, you should look into whatever form of shared memory the platform offers. A simple protocol based around shared memory and locks or semaphores is by far the fastest technique."
"If you do decide to use sockets, bind the 'server' socket to 'localhost'. On most platforms, this will take a shortcut around a couple of layers of network code and be quite a bit faster."
In
Python 3, all strings are Unicode. For more info,
visit Character Encoding.
So, if any kind of text string is to be sent across the network, it
needs to be encoded.This is why the server is using the
encode('ascii') method on the data it transmits.
Likewise, when a client receives network data, that data is first
received as raw unencoded bytes. If you print it out or try to
process it as text, we're unlikely to get what we expected.
Instead, we need to decode it first.This is why the client code is
using decode('ascii') on the result.
Echo Server
This is an echo server: the server that echoes back all data it receives to a client that sent it.
Server:
# echo_server.py import socket host = '' # Symbolic name meaning all available interfaces port = 12345 # Arbitrary non-privileged port s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.bind((host, port)) s.listen(1) conn, addr = s.accept() print('Connected by', addr) while True: data = conn.recv(1024) if not data: break conn.sendall(data) conn.close()
Client:
# echo_client.py import socket host = socket.gethostname() port = 12345 # The same port as used by the server s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.connect((host, port)) s.sendall(b'Hello, world') data = s.recv(1024) s.close() print('Received', repr(data))
Note that the server does not sendall()/recv() on the socket it is listening on but on the new socket returned by accept()
Using python (able to run in thonny), create a point of sale system that has a...
Using Python Write the following well-documented (commented) program. Create a class called Shape that has a method for printing the area and the perimeter. Create three classes (Square, Rectangle, and Circle) which inherit from it. Square will have one instance variable for the length. Rectangle will have two instance variables for the width and height. Circle will have one instance variable for the radius. The three classes will have methods for computing the area and perimeter of its corresponding shape....
I need some help with programming this assignment.
Programming Assignment 6: A Python Class, Attributes, Methods, and Objects Obiectives .Be able to write a Python class Be able to define class attributes Be able to define class methods .Be able to process input from a text file .Be able to write an application using objects The goal of this programming assignment is to develop a simple image processing application. The application will import a class that creates image objects with...
1. Think about the system that handles student admissions at our university. The primary function of the system should be able to track a student from the request for information through the admissions process until the student is either admitted to the school or rejected. II. Write a use-case description that can describe an Admit Student use case. Assume that applicants who are children of alumni are handled differently from other applicants. Also, assume that a generic Update Student Information...
OBJECTIVE:The objective of the assignment is to use object-oriented design techniques to design an application.DESCRIPTION:A small airline has just purchased a computer for its new automated reservations system. You have been hired to design an airline seating application for the FBN (Fly-By-Night) Airlines. In this portion of the assignment, the task is to do the object oriented design for the application. This system will be used only by the company and its employees. Customers will not interact with the system.Your...
You need not run Python programs on a computer in solving the following problems. Place your answers into separate "text" files using the names indicated on each problem. Please create your text files using the same text editor that you use for your .py files. Answer submitted in another file format such as .doc, .pages, .rtf, or.pdf will lose least one point per problem! [1] 3 points Use file math.txt What is the precise output from the following code? bar...
Using Java You are helping a corporation create a new system for keeping track of casinos and customers. The system will be able to record and modify customer and casino information. It will also be able to simulate games in the casino. Customer-specific requirements You can create new customers All new customers have a new customerID assigned to them, starting with 1 for the first, 2 for the second, 3 for the third, ect. New customers have names and monetary...
. Spring Breaks ‘R’ Us (SBRU) is an online travel service that books spring break trips to resorts for college students. The basic idea is to get a group of students to book a room at a resort for one of the traditional spring break weeks. SBRU contracts with dozens of resorts in key spring break destinations in Florida, Texas, the Caribbean, and Mexico. Its Web site shows information on each resort and includes prices, available rooms, and special features....
Draw the UML DIAGRAM ALSO
PLEASE DRAW THE UML DIAGRAM.ALSO
in java
should use the program in java
For this task you will create a Point3D class to represent a point that has coordinates in three dimensions labeled x, y and z. You will then use the class to perform some calculations on an array of these points. You need to draw a UML diagram for the class (Point3D) and then implement the class The Point3D class will have the...
PLEASE USE THE HEADER FILE THAT I ADDED TO THE END OF THE QUESTIONWrite this program with using C/C++, C#, Java or Python. Explain your code with comments. Create a communication information system for a company with using Red-Black Tree Method. Inthis system the users are allowed to search an employee from employee's registration number(id)and retreive the department number(id) and that employee's internal phone number. Also, if theemployee they are looking for is not in the tree, they can add it....
I need help for part B and C
1) Create a new project in NetBeans called Lab6Inheritance. Add a new Java class to the project called Person. 2) The UML class diagram for Person is as follows: Person - name: String - id: int + Person( String name, int id) + getName(): String + getido: int + display(): void 3) Add fields to Person class. 4) Add the constructor and the getters to person class. 5) Add the display() method,...