Skip to content

Instantly share code, notes, and snippets.

@nitinbhojwani
Created July 26, 2017 10:49
Show Gist options
  • Save nitinbhojwani/7f2dffd0b56fcc760d62ef4f928ffcb5 to your computer and use it in GitHub Desktop.
Save nitinbhojwani/7f2dffd0b56fcc760d62ef4f928ffcb5 to your computer and use it in GitHub Desktop.
Send message over TCP connection given host and port
def do_telnet(host, port, message):
import telnetlib
tn = telnetlib.Telnet(host, port)
ret = tn.write(message + '\n')
print "sent the message: %s" % message
tn.close()
def send_thru_socket(host, port, message):
TCP_IP = host
TCP_PORT = port
BUFFER_SIZE = 1024
MESSAGE = message
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
s.connect((TCP_IP, TCP_PORT))
except:
print 'Unable to connect'
return
s.sendall(str(MESSAGE) + '\n')
print "Sent:", MESSAGE
#data = s.recv(1)
s.close()
#print "Response:", data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment