Created
September 8, 2021 11:37
-
-
Save mustafakirimli/88a8dae4bb60a22b98149b0de2a6d9c6 to your computer and use it in GitHub Desktop.
basic socket server in Python
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 sys | |
import datetime | |
def socket_server(): | |
bind_ip = '0.0.0.0' | |
bind_port = 8000 | |
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server.bind((bind_ip, bind_port)) | |
server.listen() | |
t = datetime.datetime.now() | |
print(f'{t}: Listening on {bind_ip}:{bind_port}', flush=True) | |
while True: | |
client_socket, address = server.accept() | |
t = datetime.datetime.now() | |
print(f'{t}: Accepted connection from {address[0]}:{address[1]}', flush=True) | |
request = client_socket.recv(1024) | |
received_at = datetime.datetime.now() | |
received_data = request.decode() | |
print(f'{received_at}: Data: {received_data}', flush=True) | |
client_socket.close() | |
t = datetime.datetime.now() | |
print(f"{t}: Client disconnected: {address[0]}:{address[1]}", flush=True) | |
if __name__ == '__main__': | |
socket_server() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment