-
Notifications
You must be signed in to change notification settings - Fork 8
/
sock.py
44 lines (33 loc) · 1.36 KB
/
sock.py
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
import socket
class Socket:
"""
Handles the barest level of UDP communication with a server in a slightly
simpler way (for our purposes) than the default socket library.
"""
def __init__(self, host, port, bufsize=8192):
"""
host: hostname of the server we want to connect to
port: port of the server we want to connect to
"""
self.address = (host, port)
self.bufsize = bufsize
# the socket communication with the server takes place on (ipv4, udp)
self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
def send(self, msg, append_null_terminator=True):
"""
Sends a message to the server. Appends a null terminator by default.
"""
# append a null terminator if requested
if append_null_terminator:
msg = msg + "\0"
self.sock.sendto(msg, self.address)
def recv(self, conform_address=True):
"""
Receives data from the given socket. Returns the data as a string.
If conform_address is True, the address the server sent its response
from replaces the address and port set at object creation.
"""
data, address = self.sock.recvfrom(self.bufsize)
if conform_address:
self.address = address
return data