gpt4 book ai didi

python 3 : TCP Client/Server Broken Pipe Error

转载 作者:可可西里 更新时间:2023-11-01 02:49:26 25 4
gpt4 key购买 nike

我正在编写一个 TCP 客户端/服务器,并且在代码的后半部分遇到了这个 broken pip 错误。由于对 Python 和套接字编程的了解有限,我无法确定原因,因此无法解决问题。我将不得不包括所有代码,因为可能存在某种冲突导致我不知道的这个问题,对于长篇文章感到抱歉。

我在下面标记了我遇到问题的地方,到目前为止一切正常。

服务器代码:

import os
from socket import *
import urllib
import time

HOST = '' #We are the host
PORT = 29876
PORT2 = 29877
ADDR = (HOST, PORT)
ADDR2 = (HOST, PORT2)

BUFFSIZE = 4096


serv = socket( AF_INET,SOCK_STREAM)
serv.bind(ADDR,)
serv.listen(5)
print ('listening... \n')


conn,addr = serv.accept()
print (conn,addr)
print ('...connected \n')

with open(os.path.expanduser("~/.ssh/id_rsa.pub")) as f:
key = f.read()
conn.sendall(key)
print("Key Sent... \n")


data = conn.recv(BUFFSIZE)
with open('ip.txt', 'w') as myfile:
myfile.write(str(data))

with open("ip.txt", "r") as myfile:
ip=myfile.read().replace('\n','')
print("The client IP is: " + ip + "\n")

conn.close()


ver = socket(AF_INET,SOCK_STREAM)
ver.bind(ADDR2,)
ver.listen(5)
print('listening...\n')

build,addr = ver.accept()
print (build,addr)
print('...connected\n')

#Get Version
version = urllib.urlopen("http://p.b.r.com/pa/b/latest.txt")
print(version.read())

#IT IS SENDING THIS LAST PIECE OF DATA THAT CAUSES THE BROKEN PIPE ERROR
version = str(version.read())
ver.send(version)

客户端代码:

from socket import *
from winreg import *
import os
import socket
import platform
import string
import time

#Detect OS
os = platform.system()
print("Your system is running "+ os)

#Set Host address and port
HOST = 'xxx.xxx.xxx.xxx'
PORT = 29876
PORT2 = 29877
ADDR = (HOST,PORT)
ADDR2 = (HOST, PORT2)
BUFFSIZE = 4096


cli = socket.socket( AF_INET, SOCK_STREAM)
cli.connect(ADDR,)


#Get and send IP
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s.connect(("gmail.com",80))
ip = ((s.getsockname()[0]))
ip = ip.encode('utf-8')
cli.send(ip)
print("IP Sent... \n")

#Set received key to write to known hosts
data = cli.recv(BUFFSIZE)
with open('C:\cygwin\home\scollins\.ssh\known_hosts', 'w') as myfile:
myfile.write(str(data,'utf-8'))
print("Key Saved To Known Hosts")

#Close opened sockets
s.close()
cli.close()


ver = socket.socket( AF_INET, SOCK_STREAM)
ver.connect(ADDR2,)

#Get version/build number
if os == "Windows":
#Connect to the registry
regKey = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
subKey = OpenKey(regKey, r"SOFTWARE\R\R Client")
buildno = QueryValueEx(subKey, "Version")
else:
if os == "Darwin":
buildno = open("\Library\R\buildno")
else:
if os == "Linux":
buildno = open("\r\buildno")


print("You are running software build number " + str(buildno))


#Receive and write server software version to file. Read file and compare build number
data = ver.recv(BUFFSIZE)

#THIS NEXT PRINT COMMAND RETURNS NOTHING WHICH I ASSUME IS DUE TO NOTHING BEING RECEIVED DUE TO THE BROKEN PIPE
print(str(data))
with open('version.text', 'w') as myfile:
myfile.write(str(data,'utf-8'))

with open('version.txt', 'r') as myfile:
version = myfile.read()
print("The software on the server is version: " + version)

if buildno in version == True:
print("Your sofware version is up to date")
else:
print("Your software is out of date. Updating your software.")
os.system('')

ver.close()

感谢您提供的任何帮助。

最佳答案

好吧,我在工作时没有在这里安装 python3,但仅从代码来看,您似乎正试图在服务器部分使用服务器套接字发送一些东西。

你在 ver 上调用 accept:

build,addr = ver.accept()

然后您尝试在 ver 上发送,而不是在 build 上发送:

ver.send(version)

通常它是这样工作的:在服务器端,你有一个“服务器”套接字,你调用绑定(bind)然后接受,等待传入的连接。每次客户端连接时,accept 都会产生一个套接字来与这个特定的客户端(“客户端”套接字)对话。如果所有通信都通过服务器套接字进行,您如何拥有多个客户端并知道您正在与哪个客户端“交谈”?

代码中的第二个错误是调用 version.read() 来打印值,然后再次发送它。 read()“消耗”数据,因此第二个 read() 给出了一个空结果。

此外,您应该在循环中调用 send(),检查其返回值,以确保所有数据都已实际发送。可能会发生部分发送。

关于 python 3 : TCP Client/Server Broken Pipe Error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14939428/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com