In this lab project:You are a given a CSV file, and you should copy the file into some directory on
the server.
Using Python- you will import the CSV file and analyse the data frames (i.e. data structured as
columns and rows). To be able to do this - you need to install “pandas” on Python.
1. Write both TCP client and TCP server in Python version 3.8 or above.
2. Test the network connectivity between TCP client and TCP server.
3. Import the CSV file into the TCP server.
4. Find the number of rows and columns of the data set, and display the results on the TCP client
machine.
5. Output the 2
nd row on the TCP client machine.
6. Find the averages(mean) of the statistics for NBA players and display the results on the TCP
client machine.
To complete this task, you need to create two files + have a csv file with statistics.
First file server.py:
import socket
import pandas as pd
if __name__ == "__main__":
ip = "127.0.0.1"
port = 1234
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.bind((ip, port))
server.listen(5)
while True:
client, address = server.accept()
print(f"Connection Estabished - {address[0]}:{address[1]}")
df = pd.read_csv(r"nba.csv") # Path and name csv file, enter your
rows = str(len(df.index)) # Number of rows csv file
colomns = str(len(df.columns)) # Number of colomns csv file
second_rows = df.head()
second_rows = second_rows[1:2] # Second row from csv file
mean_salary = df['Salary'].mean() # Mean salary from csv file
client.send(bytes(rows, encoding='utf8'))
client.send(bytes(colomns, encoding='utf8'))
client.send(bytes(str(second_rows), encoding='utf8'))
client.send(bytes(str(mean_salary), encoding='utf8'))
Second file client.py:
import socket
import pandas as pd
if __name__ == "__main__":
ip = "127.0.0.1"
port = 1234
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server.connect((ip, port))
rows = server.recv(1024).decode('utf-8')
colomns = server.recv(1024).decode('utf-8')
second_rows = server.recv(1024).decode('utf-8')
mean_salary = server.recv(1024).decode('utf-8')
print('Rows: ', rows)
print('Colomns: ', colomns)
print('Second row: \n', second_rows)
print('Mean salary: ', mean_salary)
start the server.py first then client.py
Comments
Leave a comment