1

我正在编写一个 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()

感谢您提供的任何帮助。

4

1 回答 1

2

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

你在ver上调用accept:

build,addr = ver.accept()

然后你尝试发送版本,而不是构建:

ver.send(version)

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

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

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

于 2013-02-18T15:20:54.663 回答