init
This commit is contained in:
38
Connexion.py
Normal file
38
Connexion.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import socket
|
||||
class MySocket:
|
||||
"""demonstration class only
|
||||
- coded for clarity, not efficiency
|
||||
"""
|
||||
|
||||
def __init__(self, sock=None):
|
||||
if sock is None:
|
||||
self.sock = socket.socket(
|
||||
socket.AF_INET, socket.SOCK_STREAM)
|
||||
else:
|
||||
self.sock = sock
|
||||
|
||||
def connect(self, host, port):
|
||||
self.sock.connect((host, port))
|
||||
|
||||
def mysend(self, msg):
|
||||
totalsent = 0
|
||||
while totalsent < MSGLEN:
|
||||
sent = self.sock.send(msg[totalsent:])
|
||||
if sent == 0:
|
||||
raise RuntimeError("socket connection broken")
|
||||
totalsent = totalsent + sent
|
||||
|
||||
def myreceive(self):
|
||||
chunks = []
|
||||
bytes_recd = 0
|
||||
while bytes_recd < MSGLEN:
|
||||
chunk = self.sock.recv(min(MSGLEN - bytes_recd, 2048))
|
||||
if chunk == b'':
|
||||
raise RuntimeError("socket connection broken")
|
||||
chunks.append(chunk)
|
||||
bytes_recd = bytes_recd + len(chunk)
|
||||
return b''.join(chunks)
|
||||
|
||||
if __name__ == "__main__":
|
||||
print("bonjour")
|
||||
MySocket()
|
||||
Reference in New Issue
Block a user