Created
December 15, 2011 11:42
-
-
Save dketov/1480815 to your computer and use it in GitHub Desktop.
Протоколы TCP и UDP
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
# -*- encoding: utf-8 -*- | |
""" | |
Обратный DNS клиент | |
""" | |
import sys, socket | |
try: | |
result = socket.gethostbyaddr("66.249.71.15") | |
print "Primary hostname:" | |
print " " + result[0] | |
# Display the list of available addresses that is also returned | |
print "\nAddresses:" | |
for item in result[2]: | |
print " " + item | |
except socket.herror, e: | |
print "Couldn't look up name:", e |
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
# -*- encoding: utf-8 -*- | |
""" | |
TCP эхо-сервер | |
""" | |
import socket | |
HOST = '' # Symbolic name meaning all available interfaces | |
PORT = 50007 # Arbitrary non-privileged port | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.bind((HOST, PORT)) | |
s.listen(1) | |
conn, addr = s.accept() | |
print 'Connected by', addr | |
while 1: | |
data = conn.recv(1024) | |
if not data: break | |
conn.send(data) | |
conn.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
# -*- encoding: utf-8 -*- | |
""" | |
TCP клиент | |
""" | |
import socket | |
HOST = 'localhost' # The remote host | |
PORT = 50007 # The same port as used by the server | |
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) | |
s.connect((HOST, PORT)) | |
s.send('Hello, world') | |
data = s.recv(1024) | |
s.close() | |
print 'Received', repr(data) |
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
# -*- encoding: utf-8 -*- | |
""" | |
UDP эхо-сервер | |
""" | |
import socket, traceback | |
host = '' | |
port = 51423 | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) | |
s.bind((host, port)) | |
while 1: | |
try: | |
message, address = s.recvfrom(8192) | |
print "Got data from", address | |
s.sendto(message, address) | |
except (KeyboardInterrupt, SystemExit): | |
raise | |
except: | |
traceback.print_exc() |
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
# -*- encoding: utf-8 -*- | |
""" | |
UDP клиент | |
""" | |
import socket, sys, time | |
host = sys.argv[1] | |
textport = "80" | |
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) | |
port = int(textport) | |
s.connect((host, port)) | |
print "Enter data to transmit: " | |
data = sys.stdin.readline().strip() | |
s.sendall(data) | |
s.shutdown(1) | |
print "Looking for replies; press Ctrl-C or Ctrl-Break to stop." | |
while 1: | |
buf = s.recv(2048) | |
if not len(buf): | |
break | |
print "Received: %s" % buf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment