Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 插座-x27;str';对象不能解释为整数_Python 3.x_Sockets - Fatal编程技术网

Python 3.x 插座-x27;str';对象不能解释为整数

Python 3.x 插座-x27;str';对象不能解释为整数,python-3.x,sockets,Python 3.x,Sockets,下午好,我将从python中的套接字工具开始,并创建一个基本服务器。 但是,它给我带来了一个错误: “str”对象不能解释为整数 现在多次修改代码,但错误不断出现。 最后一个更改是client.sendto(msg.encode('utf-8')) 这是我的代码: 服务器: import socket ip = "0.0.0.0" puerto = 8081 dataConection = (ip,puerto) conexionesMaximas = 10 socketServidor =

下午好,我将从python中的套接字工具开始,并创建一个基本服务器。 但是,它给我带来了一个错误:
“str”对象不能解释为整数

现在多次修改代码,但错误不断出现。 最后一个更改是
client.sendto(msg.encode('utf-8'))

这是我的代码:

服务器:

import socket

ip = "0.0.0.0"
puerto = 8081
dataConection = (ip,puerto)
conexionesMaximas = 10

socketServidor = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socketServidor.bind(dataConection)
socketServidor.listen(conexionesMaximas)

print("Esperando conexiones en: ",ip,puerto)
cliente,direccion = socketServidor.accept()
print("Conexion establecida con: ",direccion[0],direccion[1])

while True:
    datos = cliente.recv(1024)
    print(datos)
    modificado,severAdress = cliente.recvfrom(datos.decode('utf-8'))
    if modificado == "exit":
        cliente.send("exit")
    print("Recibido",data)
    cliente.sendall("---Recibido---")
print("Conexion cerrada")
socketServidor.close()
客户:

import socket

ipServidor = "192.168.6.1"
puertoServidor = 8081

cliente = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
cliente.connect((ipServidor,puertoServidor))
print("conectado con: ",ipServidor,puertoServidor)

while True:
    msg = input(">: ")
    cliente.sendto(msg.encode("utf-8"),(ipServidor,puertoServidor))
    respuesta = cliente.recv(4096)
    print(respuesta)
    if respuesta == "exit":
        break;

print("----conexion cerrada----")
cliente.close()
这是行错误:

Esperando conexiones en:  0.0.0.0 8081
Conexion establecida con:  192.168.8.3 49774
b'k'
Traceback (most recent call last):
  File "C:\Users\Angel\Desktop\server.py", line 19, in <module>
    modificado,severAdress = cliente.recvfrom(datos.decode('utf-8'))
TypeError: 'str' object cannot be interpreted as an integer
Esperando conexiones en:0.0.0.0 8081
Conexion establecida con:192.168.8.3 49774
b'k'
回溯(最近一次呼叫最后一次):
文件“C:\Users\Angel\Desktop\server.py”,第19行,在
modificado,severaddress=cliente.recvfrom(datos.decode('utf-8'))
TypeError:“str”对象不能解释为整数

您正在使用类型不正确的参数调用
recvfrom
socket.recvfrom
方法接受缓冲区大小作为其第一个参数,该参数指定要接收的最大字节数。您给它一个字符串,即
datos
中的解码字节


如果确定
datos
包含字符串形式的整数,只需调用:
client.recvfrom(int(datos.decode('utf-8'))
,即可将其转换为整数

有许多错误,但主要是不需要在连接的套接字上使用
sendto
recvfrom
。编写以下代码是为了在同一台计算机上运行客户端和服务器:

server.py

import socket

ip = ""
puerto = 8081
dataConection = (ip,puerto)
conexionesMaximas = 10

socketServidor = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socketServidor.bind(dataConection)
socketServidor.listen(conexionesMaximas)

print("Esperando conexiones en: ",ip,puerto)
cliente,direccion = socketServidor.accept()
print("Conexion establecida con: ",direccion[0],direccion[1])

while True:
    datos = cliente.recv(1024).decode()
    print(datos)
    if datos == "exit":
        cliente.sendall("exit".encode())
        break
    print("Recibido",datos)
    cliente.sendall("---Recibido---".encode())
print("Conexion cerrada")
socketServidor.close()
import socket

ipServidor = "localhost"
puertoServidor = 8081

cliente = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
cliente.connect((ipServidor,puertoServidor))
print("conectado con: ",ipServidor,puertoServidor)

while True:
    msg = input(">: ")
    cliente.sendall(msg.encode())
    respuesta = cliente.recv(4096).decode()
    print(respuesta)
    if respuesta == "exit":
        break

print("----conexion cerrada----")
cliente.close()
client.py

import socket

ip = ""
puerto = 8081
dataConection = (ip,puerto)
conexionesMaximas = 10

socketServidor = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
socketServidor.bind(dataConection)
socketServidor.listen(conexionesMaximas)

print("Esperando conexiones en: ",ip,puerto)
cliente,direccion = socketServidor.accept()
print("Conexion establecida con: ",direccion[0],direccion[1])

while True:
    datos = cliente.recv(1024).decode()
    print(datos)
    if datos == "exit":
        cliente.sendall("exit".encode())
        break
    print("Recibido",datos)
    cliente.sendall("---Recibido---".encode())
print("Conexion cerrada")
socketServidor.close()
import socket

ipServidor = "localhost"
puertoServidor = 8081

cliente = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
cliente.connect((ipServidor,puertoServidor))
print("conectado con: ",ipServidor,puertoServidor)

while True:
    msg = input(">: ")
    cliente.sendall(msg.encode())
    respuesta = cliente.recv(4096).decode()
    print(respuesta)
    if respuesta == "exit":
        break

print("----conexion cerrada----")
cliente.close()

请注意,TCP套接字上的
recv(1024)
是一个没有消息边界的字节流,因此不能保证在不实现附加协议的情况下接收消息的所有字节。例如,在消息之前发送消息的长度,或者对于像这样的面向字符串的消息,读取直到接收到新行字符。对于这样的短字符串,您可能永远看不到问题,或者只有在网络非常繁忙或不可靠时才会看到问题。

请显示错误回溯。失败的是哪一行?