473,320 Members | 2,035 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Can anyone explain a part of a telnet client code to me..

Hello
I have a program that can telnet to a host.
But I cannot understand from [for c in data] part, can anyone explain
it to me?
Thank you very much.

[code]
import sys, posix, time
from socket import *

BUFSIZE = 1024

# Telnet protocol characters

IAC = chr(255) # Interpret as command
DONT = chr(254)
DO = chr(253)
WONT = chr(252)
WILL = chr(251)

def main():
# Get hostname from param
host = sys.argv[1]
try:
# Get ip from hostname
hostaddr = gethostbyname(host)
except error:
sys.stderr.write(sys.argv[1] + ': bad host name\n')
sys.exit(2)
# Check param[2] as type of protocol
if len(sys.argv) 2:
servname = sys.argv[2]
else:
# default use telnet
servname = 'telnet'
# If got servname as port num
if '0' <= servname[:1] <= '9':
# cast port num from str to int
port = eval(servname)
else:
try:
# Get port num by service name
port = getservbyname(servname, 'tcp')
except error:
sys.stderr.write(servname + ': bad tcp service name\n')
sys.exit(2)
# Create a tcp socket
s = socket(AF_INET, SOCK_STREAM)
# Connect to server
try:
s.connect((host, port))
except error, msg:
sys.stderr.write('connect failed: ' + repr(msg) + '\n')
sys.exit(1)
# Fork a proccess
pid = posix.fork()
#
if pid == 0:
# child -- read stdin, write socket
while 1:
line = sys.stdin.readline()
s.send(line)
else:
# parent -- read socket, write stdout
iac = 0 # Interpret next char as command
opt = '' # Interpret next char as option
while 1:
data = s.recv(BUFSIZE)
# if recv nothing then Exit program
if not data:
# EOF; kill child and exit
sys.stderr.write( '(Closed by remote host)\n')
# Call posix function kill and send signal 9 to child
posix.kill(pid, 9)
sys.exit(1)
cleandata = ''
for c in data:
if opt:
print ord(c)
s.send(opt + c)
opt = ''
elif iac:
iac = 0
if c == IAC:
cleandata = cleandata + c
elif c in (DO, DONT):
if c == DO: print '(DO)',
else: print '(DONT)',
opt = IAC + WONT
elif c in (WILL, WONT):
if c == WILL: print '(WILL)',
else: print '(WONT)',
opt = IAC + DONT
else:
print '(command)', ord(c)
elif c == IAC:
iac = 1
print '(IAC)',
else:
cleandata = cleandata + c
sys.stdout.write(cleandata)
sys.stdout.flush()
try:
main()
except KeyboardInterrupt:
pass

Feb 11 '07 #1
1 2083
En Sun, 11 Feb 2007 00:48:57 -0300, Jia Lu <Ro*****@gmail.comescribió:
I have a program that can telnet to a host.
But I cannot understand from [for c in data] part, can anyone explain
it to me?
data is the received string.
The for statement is used to iterate over a sequence; a string is
considered a sequence of characters, so it iterates over all the
characters in the string, one by one.

pydata = "Hello"
pyfor c in data:
.... print c, ord(c)
....
H 72
e 101
l 108
l 108
o 111
py>

--
Gabriel Genellina

Feb 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dan | last post by:
I'm writing a simplistic telnet client in VB6 and I've run into a small snag. The program has a textbox to write in the string to be sent using ..SendData and has another textbox that displays...
3
by: Yannick Turgeon | last post by:
Hello all, I'm currently trying to pass commands to a telnet session and get the texte generated (stdin + stdout) by the session. The problem I get is that the Telnet.read_until() function...
4
by: Donnal Walter | last post by:
On Windows XP I am able to connect to a remote telnet server from the command prompt using: telnet nnn.nnn.nnn.nnn 23 where nnn.nnn.nnn.nnn is the IP address of the host. But using telnetlib,...
6
by: Donnal Walter | last post by:
Several months ago I tried using the telnet module (on Windows XP) to communicate with a proprietary host on our network. This was unsuccessful due to problems with "option negotiation", and I gave...
3
by: Pete | last post by:
Is there any possiblity of writing an Access or Visual Basic application that provides a method of sharing the window focus between Access and the Shell application? i.e....
3
by: Horst Walter | last post by:
What I try to accomplish is to run "telnet.exe" as a process in C#. The C#-code below works with terminating commands, e.g. a "HelloWorld.exe". Since I'd like to communicate with "telnet" the...
7
by: Rex Winn | last post by:
I've Googled until my eyes hurt looking for a way to issue Telnet commands from C# and cannot find anything but $300 libraries that encapsulate it for you. I don't want to be able to create a...
2
by: mnsindhu74 | last post by:
I want to implement telnet client in C#. I am able to connect to the telnet server in windows XP and do the negotiations. I also get the login prompt.After I send the username I get password...
2
by: thilandeneth | last post by:
i need to do telnet via a web server please give me a idia to initiate the project following requirements are needed 1 Create web based custom telnet client to communicate with remote...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.