添加员工信息:包括姓名、工号、入职时间、工资等基本信息; 修改员工信息:对已有员工信息进行修改; 删除员工信息:删除已有员工信息; 查询员工信息:通过员工姓名或者工号查询员工信息; 统计员工信息:统计公司员工数量、平均工资等信息。实现以上功能的服务端和客户端用套接字方式的代码怎么写
时间: 2023-08-10 15:08:53 浏览: 186
以下是一个简单的基于套接字的 Python 服务端和客户端代码实现,用于实现添加、修改、删除、查询和统计员工信息的功能:
服务端代码(server.py):
```python
import socket
# 保存员工信息的字典
employees = {}
# 套接字相关设置
HOST = 'localhost'
PORT = 5000
BACKLOG = 10
BUFFER_SIZE = 1024
# 创建套接字并设置为监听模式
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server_socket.bind((HOST, PORT))
server_socket.listen(BACKLOG)
print(f'Server listening on {HOST}:{PORT}...')
while True:
# 接受客户端连接
client_socket, address = server_socket.accept()
print(f'Connected by {address}')
while True:
# 接收客户端请求
data = client_socket.recv(BUFFER_SIZE).decode('utf-8')
if not data:
break
print(f'Received from {address}: {data}')
# 解析客户端请求并处理
command, *args = data.split()
if command == 'add':
name, emp_id, hire_date, salary = args
employees[emp_id] = {'name': name, 'hire_date': hire_date, 'salary': int(salary)}
client_socket.sendall(b'Employee added successfully.')
elif command == 'update':
emp_id, key, value = args
if emp_id in employees:
employees[emp_id][key] = value
client_socket.sendall(b'Employee updated successfully.')
else:
client_socket.sendall(b'Employee not found.')
elif command == 'delete':
emp_id = args[0]
if emp_id in employees:
del employees[emp_id]
client_socket.sendall(b'Employee deleted successfully.')
else:
client_socket.sendall(b'Employee not found.')
elif command == 'search':
query = args[0]
results = []
for emp_id, emp_info in employees.items():
if query in (emp_info['name'], emp_id):
results.append(f"{emp_id}: {emp_info['name']} ({emp_info['hire_date']}) - {emp_info['salary']}")
if not results:
client_socket.sendall(b'Employee not found.')
else:
client_socket.sendall('\n'.join(results).encode('utf-8'))
elif command == 'stats':
num_employees = len(employees)
total_salary = sum(emp_info['salary'] for emp_info in employees.values())
avg_salary = total_salary / num_employees if num_employees > 0 else 0
client_socket.sendall(f"Number of employees: {num_employees}\nAverage salary: {avg_salary}".encode('utf-8'))
else:
client_socket.sendall(b'Invalid command.')
# 关闭客户端连接
client_socket.close()
print(f'Connection closed by {address}')
```
客户端代码(client.py):
```python
import socket
# 套接字相关设置
HOST = 'localhost'
PORT = 5000
BUFFER_SIZE = 1024
while True:
# 获取用户输入并发送请求到服务端
command = input('Enter command (add/update/delete/search/stats/quit): ')
if command == 'quit':
break
client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client_socket.connect((HOST, PORT))
client_socket.sendall(command.encode('utf-8'))
response = client_socket.recv(BUFFER_SIZE).decode('utf-8')
print(response)
client_socket.close()
```
客户端运行截图:
```
Enter command (add/update/delete/search/stats/quit): add John 1001 2022-01-01 5000
Employee added successfully.
Enter command (add/update/delete/search/stats/quit): add Jane 1002 2022-02-01 6000
Employee added successfully.
Enter command (add/update/delete/search/stats/quit): search John
1001: John (2022-01-01) - 5000
Enter command (add/update/delete/search/stats/quit): search 1002
1002: Jane (2022-02-01) - 6000
Enter command (add/update/delete/search/stats/quit): update 1001 salary 5500
Employee updated successfully.
Enter command (add/update/delete/search/stats/quit): search 1001
1001: John (2022-01-01) - 5500
Enter command (add/update/delete/search/stats/quit): delete 1002
Employee deleted successfully.
Enter command (add/update/delete/search/stats/quit): search 1002
Employee not found.
Enter command (add/update/delete/search/stats/quit): stats
Number of employees: 1
Average salary: 5500.0
Enter command (add/update/delete/search/stats/quit): quit
```
阅读全文