Created
October 21, 2018 23:31
-
-
Save JohnPhamous/8d9d0353105e526088f3bf2d52d7fe37 to your computer and use it in GitHub Desktop.
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
| from socket import * | |
| client = socket(AF_INET, SOCK_STREAM) | |
| print('Socket Created') |
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
| from socket import * | |
| import sys | |
| try: | |
| client = socket(AF_INET, SOCK_STREAM) | |
| except error, msg: | |
| print('Failed to create socket, error code: {} {}'.format(msg[0], msg[1])) | |
| sys.exit() | |
| print('Socket Created') |
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
| from socket import * | |
| import sys | |
| try: | |
| client = socket(AF_INET, SOCK_STREAM) | |
| except error, msg: | |
| print('Failed to create socket, error code: {} {}'.format(msg[0], msg[1])) | |
| sys.exit() | |
| host = 'www.biblegateway.com' | |
| port = 80 | |
| try: | |
| host_ip = gethostbyname(host) | |
| except gaierror: | |
| print('Could not resolve hostname') | |
| sys.exit() | |
| print('The IP of {} is {}'.format(host, host_ip)) | |
| client.connect((host_ip, port)) | |
| print('Connected to {} ({}) on port {}'.format(host, host_ip, port)) |
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
| from socket import * | |
| import sys | |
| try: | |
| client = socket(AF_INET, SOCK_STREAM) | |
| except error, msg: | |
| print('Failed to create socket, error code: {} {}'.format(msg[0], msg[1])) | |
| sys.exit() | |
| host = 'www.biblegateway.com' | |
| port = 80 | |
| try: | |
| host_ip = gethostbyname(host) | |
| except gaierror: | |
| print('Could not resolve hostname') | |
| sys.exit() | |
| print('The IP of {} is {}'.format(host, host_ip)) | |
| client.connect((host_ip, port)) | |
| print('Connected to {} ({}) on port {}'.format(host, host_ip, port)) | |
| message = 'GET / HTTP/1.1\r\n\r\n' | |
| try: | |
| client.sendall(message) | |
| except error: | |
| print('Send failed: {}'.format(error)) | |
| sys.exit() | |
| print('Message sent sucessfully') |
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
| from socket import * | |
| import sys | |
| try: | |
| client = socket(AF_INET, SOCK_STREAM) | |
| except error, msg: | |
| print('Failed to create socket, error code: {} {}'.format(msg[0], msg[1])) | |
| sys.exit() | |
| host = 'www.biblegateway.com' | |
| port = 80 | |
| try: | |
| host_ip = gethostbyname(host) | |
| except gaierror: | |
| print('Could not resolve hostname') | |
| sys.exit() | |
| print('The IP of {} is {}'.format(host, host_ip)) | |
| client.connect((host_ip, port)) | |
| print('Connected to {} ({}) on port {}'.format(host, host_ip, port)) | |
| message = 'GET / HTTP/2.0\r\n\r\n' | |
| try: | |
| client.sendall(message) | |
| except error: | |
| print('Send failed: {}'.format(error)) | |
| sys.exit() | |
| print('Message sent sucessfully') | |
| reply = client.recv(4096) | |
| print(reply) | |
| client.close() |
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
| from socket import * | |
| import sys | |
| host = '' | |
| port = 3983 | |
| server = socket(AF_INET, SOCK_STREAM) | |
| print('Server created, listening on {}'.format(port)) | |
| try: | |
| server.bind((host, port)) | |
| except error, msg: | |
| print('Socket binding failed: {} {}'.format(msg[0], msg[1])) | |
| sys.exit() | |
| print('Socket binding complete') | |
| server.listen(10) | |
| print('Socket is now listening') | |
| connection, address = server.accept() | |
| print('Connected with {} {}'.format(address[0], address[1])) | |
| data = connection.recv(1024) | |
| connection.sendall(data) | |
| connection.close() | |
| server.close() |
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
| from socket import * | |
| import sys | |
| host = '' | |
| port = 3983 | |
| server = socket(AF_INET, SOCK_STREAM) | |
| print('Server created, listening on {}'.format(port)) | |
| try: | |
| server.bind((host, port)) | |
| except error, msg: | |
| print('Socket binding failed: {} {}'.format(msg[0], msg[1])) | |
| sys.exit() | |
| print('Socket binding complete') | |
| server.listen(10) | |
| print('Socket is now listening') | |
| while 1: | |
| connection, address = server.accept() | |
| print('Connected with {} {}'.format(address[0], address[1])) | |
| data = connection.recv(1024) | |
| if not data: | |
| break | |
| connection.sendall('OK...{}'.format(data)) | |
| connection.close() | |
| server.close() |
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
| from socket import * | |
| from thread import * | |
| host = '' | |
| port = 3983 | |
| s = socket(AF_INET, SOCK_STREAM) | |
| s.bind((host, port)) | |
| s.listen(10) | |
| def client_thread(c): | |
| c.send('Wecome to the server, send me something') | |
| while True: | |
| data = c.recv(1024) | |
| if not data: | |
| break | |
| c.sendall('OK...{}'.format(data)) | |
| c.close() | |
| while 1: | |
| conn, addr = s.accept() | |
| print('Connected with {}:{}'.format(addr[0], addr[1])) | |
| start_new_thread(client_thread, (conn,)) | |
| s.close() |
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
| telnet localhost 3983 |
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
| from socket import * | |
| from thread import * | |
| host = '' | |
| port = 3983 | |
| s = socket(AF_INET, SOCK_STREAM) | |
| s.bind((host, port)) | |
| s.listen(10) | |
| clients = [] | |
| def client_thread(c): | |
| try: | |
| clients.append(c) | |
| c.send('Wecome to the server, send me something: ') | |
| while True: | |
| data = c.recv(1024) | |
| if not data: | |
| break | |
| if '!q' in data: | |
| c.close() | |
| for index, client in enumerate(clients): | |
| if c == client: | |
| del clients[index] | |
| if data.split(' ')[0] == '!sendall': | |
| for client in clients: | |
| client.sendall('{}'.format(''.join(data.split(' ')[1:]))) | |
| else: | |
| c.sendall('OK...{}'.format(data)) | |
| except: | |
| print('Client disconnected') | |
| while 1: | |
| conn, addr = s.accept() | |
| print('Connected with {}:{}'.format(addr[0], addr[1])) | |
| start_new_thread(client_thread, (conn,)) | |
| s.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment