python while break try 无法中断_解码不能正常工作并且while循环不能正确中断python...
這是一個使用Python中的TCP套接字的文件傳輸應(yīng)用程序。文件傳輸協(xié)議將包含一個名為ftps.py文件一個客戶打電話來ftpc.py文件. 在
文件傳輸應(yīng)用程序?qū)⑹褂靡粋€簡單的協(xié)議。前4個字節(jié)(按網(wǎng)絡(luò)字節(jié)順序)將包含后續(xù)文件中的字節(jié)數(shù)。接下來的20個字節(jié)將包含文件的名稱(假設(shè)名稱可以容納20個字節(jié))。接下來的TCP流中的其余字節(jié)將包含文件中的數(shù)據(jù)。在
我在不同的計算機上運行客戶機程序和服務(wù)器程序,但有兩個錯誤:
1) 當(dāng)我編碼文件名并將其發(fā)送到套接字時,當(dāng)服務(wù)器端接收并解碼時,文件名是不正確的
2)服務(wù)器端似乎陷入了while循環(huán),我不知道為什么
客戶import socket
import sys
import os
filename=sys.argv[2]
print filename
if len(sys.argv) > 1 :
print('Trying to connect...')
s = socket.socket()
ip_add=(sys.argv[1])
print(ip_add)
s.connect((ip_add, 4000))
print('Connected. Wating for command.')
while True:
cmd = s.recv(24)
if cmd == 'getsize':
print('"getsize" command received.')
statinfo=os.stat(filename)
byte_size=statinfo.st_size
size_str=str(byte_size)
s.sendall(size_str)
if cmd == 'getfilename':
print('"getfilename" command received.')
formatted_name = filename.rjust(20)
fname = formatted_name.lstrip()
print(fname)
encoded_filename=fname.encode('utf-8')
s.sendall(encoded_filename)
if cmd == 'getfile':
print('"getfile" command received. Going to send file.')
file_input = open(sys.argv[2], 'rb')
while True:
buff = file_input.read(512)
if len(buff) == 0:
break
s.sendall(buff)
print('File transmission done.')
if cmd == 'end':
print('"end" command received. Teminate.')
break
file_input.close()
f.close()
服務(wù)器
^{pr2}$
以下是服務(wù)器程序的終端輸出:% python ftps.py
Waiting for clinet to connect...
Connected. Going to receive file.
1797
Filename: 55
客戶端輸出:% python ftpc.py 164.107.113.20 2.jpg
2.jpg
Trying to connect...
164.107.113.20
Connected. Wating for command.
"getsize" command received.
"getfilename" command received.
2.jpg
"getfile" command received. Going to send file.
File transmission done.
文件名:55,應(yīng)該是文件名:2.jpg
程序在這一點上陷入了一個循環(huán)。。有人知道為什么會這樣嗎?在
總結(jié)
以上是生活随笔為你收集整理的python while break try 无法中断_解码不能正常工作并且while循环不能正确中断python...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 脾氨肽多少钱一盒啊?
- 下一篇: python运行结果图_[宜配屋]听图阁