本文整理汇总了Python中message.Message.Body方法的典型用法代码示例。如果您正苦于以下问题:Python Message.Body方法的具体用法?Python Message.Body怎么用?Python Message.Body使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类message.Message
的用法示例。
在下文中一共展示了Message.Body方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: receive
# 需要导入模块: from message import Message [as 别名]
# 或者: from message.Message import Body [as 别名]
def receive(sock):
totalRecv = 0
sizeToRead = 16 # 헤더의 크기
hBuffer = bytes() # 헤더 버퍼
# 헤더 읽기
while sizeToRead > 0:
buffer = sock.recv(sizeToRead)
if len(buffer) == 0:
return None
hBuffer += buffer
totalRecv += len(buffer)
sizeToRead -= len(buffer)
header = Header(hBuffer)
totalRecv = 0
bBuffer = bytes()
sizeToRead = header.BODYLEN
while sizeToRead > 0:
buffer = sock.recv(sizeToRead)
if len(buffer) == 0:
return None
bBuffer += buffer
totalRecv += len(buffer)
sizeToRead -= len(buffer)
body = None
if header.MSGTYPE == message.REQ_FILE_SEND:
body = BodyRequest(bBuffer)
elif header.MSGTYPE == message.REP_FILE_SEND:
body = BodyResponse(bBuffer)
elif header.MSGTYPE == message.FILE_SEND_DATA:
body = BodyData(bBuffer)
elif header.MSGTYPE == message.FILE_SEND_RES:
body = BodyResult(bBuffer)
else:
raise Exception(
"Unknown MSGTYPE : {0}".
format(header.MSGTYPE))
msg = Message()
msg.Header = header
msg.Body = body
return msg
示例2: handle
# 需要导入模块: from message import Message [as 别名]
# 或者: from message.Message import Body [as 别名]
def handle(self):
print("클라이언트 접속 : {0}".format(self.client_address[0]))
client = self.request # client socket
reqMsg = MessageUtil.receive(client)
if reqMsg.Header.MSGTYPE != message.REQ_FILE_SEND:
client.close()
return
reqBody = BodyRequest(None)
print(
"파일 업로드 요청이 왔습니다. 수락하시겠습니까? yes/no")
answer = sys.stdin.readline()
rspMsg = Message()
rspMsg.Body = BodyResponse(None)
rspMsg.Body.MSGID = reqMsg.Header.MSGID
rspMsg.Body.RESPONSE = message.ACCEPTED
rspMsg.Header = Header(None)
msgId = 0
rspMsg.Header.MSGID = msgId
msgId = msgId + 1
rspMsg.Header.MSGTYPE = message.REP_FILE_SEND
rspMsg.Header.BODYLEN = rspMsg.Body.GetSize()
rspMsg.Header.FRAGMENTED = message.NOT_FRAGMENTED
rspMsg.Header.LASTMSG = message.LASTMSG
rspMsg.Header.SEQ = 0
if answer.strip() != "yes":
rspMsg.Body = BodyResponse(None)
rspMsg.Body.MSGID = reqMsg.Header.MSGID
rspMsg.Body.RESPONSE = message.DENIED
MessageUtil.send(client, rspMsg)
client.close()
return
else:
MessageUtil.send(client, rspMsg)
print("파일 전송을 시작합니다...")
fileSize = reqMsg.Body.FILESIZE
fileName = reqMsg.Body.FILENAME
recvFileSize = 0
with open(upload_dir + "\\" + fileName, 'wb') as file:
dataMsgId = -1
prevSeq = 0
while True:
reqMsg = MessageUtil.receive(client)
if reqMsg == None:
break
print("#", end='')
if reqMsg.Header.MSGTYPE != message.FILE_SEND_DATA:
break
if dataMsgId == -1:
dataMsgId = reqMsg.Header.MSGID
elif dataMsgId != reqMsg.Header.MSGID:
break
if prevSeq != reqMsg.Header.SEQ:
print("{0}, {1}".format(prevSeq, reqMsg.Header.SEQ))
break
prevSeq += 1
recvFileSize += reqMsg.Body.GetSize()
file.write(reqMsg.Body.GetBytes())
if reqMsg.Header.LASTMSG == message.LASTMSG:
break
file.close()
print()
print("수신 파일 크기 : {0} bytes".format(recvFileSize))
rstMsg = Message()
rstMsg.Body = BodyResult(None)
rstMsg.Body.MSGID = reqMsg.Header.MSGID
rstMsg.Body.RESULT = message.SUCCESS
rstMsg.Header = Header(None)
rstMsg.Header.MSGID = msgId
msgId += 1
rstMsg.Header.MSGTYPE = message.FILE_SEND_RES
rstMsg.Header.BODYLEN = rstMsg.Body.GetSize()
rstMsg.Header.FRAGMENTED = message.NOT_FRAGMENTED
rstMsg.Header.LASTMSG = message.LASTMSG
rstMsg.Header.SEQ = 0
if fileSize == recvFileSize:
#.........这里部分代码省略.........
示例3: print
# 需要导入模块: from message import Message [as 别名]
# 或者: from message.Message import Body [as 别名]
serverIp = sys.argv[1]
serverPort = 5425
filepath = sys.argv[2]
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
print("서버:{0}/{1}".format(serverIp, serverPort))
sock.connect((serverIp, serverPort))
msgId = 0
reqMsg = Message()
filesize = os.path.getsize(filepath)
reqMsg.Body = BodyRequest(None)
reqMsg.Body.FILESIZE = filesize
reqMsg.Body.FILENAME = filepath[filepath.rindex('\\')+1:]
msgId += 1
reqMsg.Header = Header(None)
reqMsg.Header.MSGID = msgId
reqMsg.Header.MSGTYPE = message.REQ_FILE_SEND
reqMsg.Header.BODYLEN = reqMsg.Body.GetSize()
reqMsg.Header.FRAGMENTED = message.NOT_FRAGMENTED
reqMsg.Header.LASTMSG = message.LASTMSG
reqMsg.Header.SEQ = 0
MessageUtil.send(sock, reqMsg)
rspMsg = MessageUtil.receive(sock)