Last active
July 18, 2018 17:47
-
-
Save juagargi/89a0d6737c2f8bdebd506279ddd377b3 to your computer and use it in GitHub Desktop.
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 | |
server_ip = '127.0.0.1' | |
server_port = 9090 | |
def main(ipport): | |
server_ip, server_port = ipport.split(":") | |
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
client.connect((server_ip, int(server_port))) | |
client.send(b'this is a test') | |
response = client.recv(9000) | |
if __name__ == "__main__": | |
if len(sys.argv) != 2: | |
print("Usage: python3 thisthing.py ip:port") | |
sys.exit(1) | |
main(sys.argv[1]) |
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 | |
# bind_ip = '0.0.0.0' | |
# bind_ip = '127.0.0.1' | |
bind_ip = '192.168.0.100' | |
bind_port = 9090 | |
server_socket = None | |
def handle_client(client_sock, address): | |
print('New client from %s' % str(address)) | |
request = client_sock.recv(9000) | |
client_sock.close() | |
def main(ipport): | |
global server_socket | |
bind_ip, bind_port = ipport.split(":") | |
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
server_socket.bind((bind_ip, int(bind_port))) | |
server_socket.listen(1) # backlog of 1 connection | |
print("serving on {}:{}".format(bind_ip, bind_port)) | |
while True: | |
client_sock, address = server_socket.accept() | |
handle_client(client_sock=client_sock, address=address) | |
if __name__ == "__main__": | |
try: | |
if len(sys.argv) != 2: | |
print("Usage: python3 thisthing.py ip:port") | |
sys.exit(1) | |
main(sys.argv[1]) | |
except KeyboardInterrupt: | |
# print(ex) | |
if server_socket: | |
server_socket.close() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment