-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPython_Socket_Programming_Using_Socket.py
More file actions
51 lines (40 loc) · 1.72 KB
/
Copy pathPython_Socket_Programming_Using_Socket.py
File metadata and controls
51 lines (40 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# Python Socket Programming.
# Of the various forms of Inter Process Communication (IPC), sockets are by far the most popular.
# The combination of sockets with INET makes talking to arbitrary machines around the world unbelievably
# easy.
# The client application (your browser, for example) uses “client” sockets for client-server communication;
# the web server it’s talking to uses both “server” sockets and “client” sockets.
# socketserver — A framework for network servers
# Using a Socket
# There are two sets of verbs to use for communication.
# You can use 'send' and 'recv', or you can transform your client socket into a file-like beast and use 'read'
# and 'write'.
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)