私は 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()
ご協力ありがとうございました。