Simple Python TCP IP script but won't return data

Question:

The objective of my script is simple! I just want a script that will log into my server at mysite.com:1000, run a command, bring back the result of that command and print it.

You can try it for yourself:

Open Putty

Connect to: mysite.com

Port: 1000

Type: RAW

Username: test

Password: test

Here is the script

import socket    # used for TCP/IP communication 

 
# Prepare for transmission
TCP_IP = 'mysite.com'
TCP_PORT = 1000
BUFFER_SIZE = 1024

#Log in as user "test" password "test" and run "/stats"
MESSAGE = 'ntestntestn/stats'

 
# Open socket, send message, close socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
print "Connectedn"
s.send(MESSAGE)
print "Message Sentn"
data = s.recv(BUFFER_SIZE)
print "Data Recvd:n"
print data
s.close()
print "Socket closed"

When the script runs, it returns:

Connected

Message Sent

Data Recvd:


Socket closed

No data is received.

Any ideas?

Thanks

EDIT:

I’m getting data back now (Thanks!!), but still not able to login

New Script:

import socket    # used for TCP/IP communication 
import time
 
# Prepare for transmission
TCP_IP = 'mysite.com'
TCP_PORT = 1000
BUFFER_SIZE = 2048

#Log in as user "test" password "test" and run "/config"
MESSAGE1 = 'rntest'
MESSAGE2 = 'rntest'
MESSAGE3 = 'rn/stats'
 
# Open socket, send message, close socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
print "Connectedn"
s.send(MESSAGE1)
time.sleep(1) # delays for 1 second
s.send(MESSAGE2)
time.sleep(1)
s.send(MESSAGE3)
time.sleep(1)
print "Message Sentn"
data = s.recv(BUFFER_SIZE)
print "Data Recvd:n"
print data
s.close()
print "Socket closed"

When I run the script:
Connected

Message Sent

Data Recvd:

Connection from [1.1.1.1]

Welcome to mysite.com Server

Enter your account name and password.


Username: 
Password: 
Socket closed
Asked By: Reilly Chase

||

Answers:

import socket    # used for TCP/IP communication 


# Prepare for transmission
TCP_IP = 'ip.com'
TCP_PORT = 10000
BUFFER_SIZE = 1024

#Log in as user "test" password "test" and run "/stats"
MESSAGE = 'rn'


# Open socket, send message, close socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((TCP_IP, TCP_PORT))
print "Connectedn"
s.send(MESSAGE)
print "Message Sentn"
data = s.recv(BUFFER_SIZE)
print "Data Recvd:n"
print data
#ADDED THESE LINES AS WELL
s.send("testrn")
data = s.recv(BUFFER_SIZE)
print "Data Recvd:n"
print data
s.close()
print "Socket closed"

Should guide you enough to do the rest yourself. Just send the data in seperate chunks

Answered By: Yurippenet
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.