Python 3:TCP客户端/服务器管道错误

时间:2013-02-18 15:12:38

标签: python tcp python-3.x client-server broken-pipe

我正在编写一个TCP客户端/服务器,我在代码的后半部分遇到了这个破坏的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()

感谢您提供的任何帮助。

1 个答案:

答案 0 :(得分:2)

好吧,我在这里没有安装python3,但只是从查看代码,看起来你正试图在服务器部分发送一些服务器套接字。

你在ver上调用accept:

build,addr = ver.accept()

然后你尝试发送ver,而不是build:

ver.send(version)

通常它的工作原理如下: 在服务器端,您有一个“服务器”套接字,您可以调用bind然后接受,等待传入连接。每次客户端连接时,接受都会生成一个套接字以与此特定客户端(“客户端”套接字)通信。如果所有通信都通过服务器套接字进行,你怎么能有多个客户端并知道你正在“讨论”哪一个?

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

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