471,872 Members | 1,337 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

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

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.

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]
# Get ip from hostname
hostaddr = gethostbyname(host)
except error:
sys.stderr.write(sys.argv[1] + ': bad host name\n')
# Check param[2] as type of protocol
if len(sys.argv) 2:
servname = sys.argv[2]
# 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)
# Get port num by service name
port = getservbyname(servname, 'tcp')
except error:
sys.stderr.write(servname + ': bad tcp service name\n')
# Create a tcp socket
s = socket(AF_INET, SOCK_STREAM)
# Connect to server
s.connect((host, port))
except error, msg:
sys.stderr.write('connect failed: ' + repr(msg) + '\n')
# Fork a proccess
pid = posix.fork()
if pid == 0:
# child -- read stdin, write socket
while 1:
line = sys.stdin.readline()
# 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)
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
print '(command)', ord(c)
elif c == IAC:
iac = 1
print '(IAC)',
cleandata = cleandata + c
except KeyboardInterrupt:

Feb 11 '07 #1
1 1965
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

Gabriel Genellina

Feb 11 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Yannick Turgeon | last post: by
4 posts views Thread by Donnal Walter | last post: by
6 posts views Thread by Donnal Walter | last post: by
3 posts views Thread by Horst Walter | last post: by
2 posts views Thread by mnsindhu74 | last post: by
reply views Thread by NeoPa | last post: by
reply views Thread by aboka | last post: by

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.