我正在编写一个 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()
感谢您提供的任何帮助。