Last active
July 3, 2019 09:59
-
-
Save TheProjectsGuy/3c2dba707e921f302ad84a7a9429b575 to your computer and use it in GitHub Desktop.
A dummy socket that acts as a server through which you can send and receive data. Built using socket programming in Python 3.5.
This file contains hidden or 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 | |
| IP_ADDR = "127.0.0.1" | |
| SOCKET_PORT = 5000 | |
| with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as skt: | |
| skt.bind((IP_ADDR, SOCKET_PORT)) | |
| skt.listen(1) | |
| (conn_skt, conn_addr) = skt.accept() | |
| with conn_skt: | |
| print("Connected to %s" % (conn_addr,)) | |
| while True: | |
| usr_ch = input("Send or receive [s/r]? (0 to exit): ") | |
| if usr_ch == 's': | |
| send_msg = input("Enter a message to send: ") | |
| send_msg = bytes(send_msg, "utf-8") | |
| conn_skt.sendall(send_msg) | |
| elif usr_ch == 'r': | |
| recv_msg = conn_skt.recv(1024) | |
| recv_msg = str(recv_msg, "utf-8") | |
| print("Received: %s" % (recv_msg, )) | |
| elif usr_ch == '0': | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment