如何修复客户端/服务器中的错误(已修复)



我想从我的列表中挑选一个随机元素,所以我使用random.sample(output, 1)然后删除它,但在删除它之前,我首先将这个元素转移到客户端进程。我想做的是让客户端确认它是否想要所述元素,如果它说不,那么不要删除它。我试图实现这个,但我在运行服务器时没有遇到错误,但是在我运行客户端进程并且它连接到服务器之后。真的很想让它发挥作用,所以非常感谢您的帮助!

此外,给出的错误不在连接本身,而是在它们之间的数据传输中,在服务器端的thread_client函数中,特别是在定义股票变量时。它说AttributeError:"builtin_function_or_method"对象没有属性"sample">

服务器代码:

import socket
from _thread import *
import random
PORT = 5050
HOST = socket.gethostbyname(socket.gethostname())
ADDR = (HOST, PORT)
ThreadCount = 0
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
records = [('Empresa:', 'Altri', 'Cotação:', 5.37), ('Empresa:', 'BCP', 'Cotação:', 0.13),
('Empresa:', 'Corticeira Amorim', 'Cotação:', 10.58), ('Empresa:', 'CTT', 'Cotação:', 5.12),
('Empresa:', 'EDP Renováveis', 'Cotação:', 20.64),
('Empresa:', 'EDP', 'Cotação:', 4.67), ('Empresa:', 'Galp', 'Cotação:', 8.70),
('Empresa:', 'Ibersol', 'Cotação:', 5.82), ('Empresa:', 'Jerónimo Martins', 'Cotação:', 16.83),
('Empresa:', 'Mota-Engil', 'Cotação:', 1.35),
('Empresa:', 'NOS', 'Cotação:', 3.03), ('Empresa:', 'Novabase', 'Cotação:', 4.60),
('Empresa:', 'PHAROL', 'Cotação:', 0.11), ('Empresa:', 'REN', 'Cotação:', 2.37),
('Empresa:', 'Semapa', 'Cotação:', 11.50), ('Empresa:', 'Sonae', 'Cotação:', 0.80),
('Empresa:', 'Sonac', 'Cotação:', 0.77), ('Empresa:', 'The Navigator Company', 'Cotação:', 2.96)]

def create_random_numbs(input_list):
output_numbers = []
for given_number in input_list:
max = given_number * 1.25
min = given_number * 0.75
random_number = random.random()
random_number_in_desired_range = min + (random_number * (max - min))
output_numbers.append(random_number_in_desired_range)
return output_numbers

def swap():
input_numbers = [x[-1] for x in records]
new_numbers_in_desired_range = create_random_numbs(input_numbers)
new_records = []
for item, new_number in zip(records, new_numbers_in_desired_range):
item_as_list = list(item)
item_as_list[-1] = new_number
new_records.append(tuple(item_as_list))
return new_records

output = swap()
swap()
try:
tcp.bind(ADDR)
except socket.error as e:
print(str(e))
print("A aguardar ligação")
tcp.listen(10)
PORT = 5050
HOST = socket.gethostbyname(socket.gethostname())
ADDR = (HOST, PORT)
ThreadCount = 0
tcp = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
tcp.bind(ADDR)
except socket.error as e:
print(str(e))
print("Awaiting conn")
tcp.listen(10)
def threaded_client(connection):
connection.send(str.encode('Bem vindo à bolsa'))
stock = random.sample(output, 1)
order = str(stock)
while True:
data = connection.recv(2048)
if not data:
break
print(data.decode('utf-8'))
tcp.send(str.encode(('Do you want this stock?' + order)))
reply = connection.recv(2048)
print(reply.decode('utf-8'))
if (reply == 'Y'):
tcp.send(str.encode(order))
output.remove(order)
elif (reply == 'N'):
break
else:
tcp.send(str.encode('Insert valid letter'))
connection.close()

客户代码:

import socket
Client = socket.socket(socket.AF_INET, socket. SOCK_STREAM)
PORT = 5050
HOST = socket.gethostbyname(socket.gethostname())
ADDR = (HOST, PORT)
compras = []
print('Awaiting conn')
try:
Client.connect(ADDR)
except socket.error as e:
print(str(e))
Response = Client.recv(1024)
while True:
Client.send(str.encode('I wanna trade!'))
Response = Client.recv(1024)
print(Response.decode('utf-8'))
Check = input('Y/N: ')
Client.send(str.encode(Check))
final = Client.recv(1024)
compras.append(final.decode('utf-8'))
Client.close()

您只向我们展示了部分代码,所以我们不能做太多。我猜您有from random import random,这就是导致AttributeError的原因。它导入的是随机函数,而不是随机模块。你需要import random

考虑到这个错误,您的服务器代码都没有运行过。您将encode拼错为enconde。您正在从output获取样品。但您正在将其从records中删除。

可以想象,这不是一个很好的方法来进行客户端/服务器设置。你不想反复交换东西。

相关内容

  • 没有找到相关文章

最新更新