Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
CSE5300/mysocket.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
30 lines (27 sloc)
756 Bytes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import socket | |
import time | |
def send_message(socket, msg): | |
total_bytes_written = 0 | |
msg_len = len(msg) | |
while total_bytes_written < msg_len: | |
bytes_written = socket.send(msg) | |
if bytes_written == 0: | |
return False | |
total_bytes_written += bytes_written | |
return socket.send("\n") == 1 | |
def receive_message(socket, timed=False): | |
message = '' | |
start_time = time.time() | |
download_time = 0 | |
while True: | |
chunk = socket.recv(1024) | |
if chunk == '': | |
return 0 | |
message += chunk | |
if message[-1] == "\n": | |
download_time = time.time() - start_time | |
break | |
if timed: | |
return message[:-1], download_time | |
else: | |
return message[:-1] |