-
Notifications
You must be signed in to change notification settings - Fork 1
/
utils.py
31 lines (25 loc) · 801 Bytes
/
utils.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
#!/usr/bin/python
# -*- coding: utf-8 -*-
# Author: Hao Luo
"""This module contains some utility functions."""
import socket
def datetime_to_str(time):
"""convert python datetime object to a
{hour}:{min}:{second}:{millisecond} string format
"""
return '{hour}:{min}:{second}:{millisecond}'.format(
hour=time.hour,
min=time.minute,
second=time.second,
millisecond=str(int(round(time.microsecond / 1000.0))),
)
def create_server_socket(port):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind(('0.0.0.0', port))
s.listen(5)
return s
def create_client_socket():
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2)
return s