473,669 Members | 2,386 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(h ost)
except error:
sys.stderr.writ e(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(s ervname, 'tcp')
except error:
sys.stderr.writ e(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.writ e('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.readl ine()
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.writ e( '(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.writ e(cleandata)
sys.stdout.flus h()
try:
main()
except KeyboardInterru pt:
pass

Feb 11 '07 #1
1 2100
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
50297
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 what that server sends. When I first connect to the server (in this case, my university's smtp server), I get a response that the server acknowledges my connection. When I type something into the textbox and send it, however, I get no response...
3
8658
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 seems to freeze after a couple of command. I did a simplify script that reproduce the problem each time (I'm using 2.3.4 on W2K):
4
11162
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, this code returns the traceback that follows: import telnetlib host = 'nnn.nnn.nnn.nnn'
6
5220
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 up on the project for a while. I still have need for this, however, so I recently started thinking about alternatives. I suppose I could dig deep enough into option negotiation to use the socket module (with telnet as a guide), but I am hoping...
3
2547
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. Shell("c:\windows\calculator.exe", vbNormalNoFocus) In other words are there Win2000/XP api's that would allow you to control the functionality of the calculator. I'm just using the calculator application as an example I am actually trying to control a main frame app...
3
11642
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 process is still running when I already have to read from the stream. This seems to be the problem, since "myStreamReader.ReadLine()" is waiting for something.
7
14089
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 Telnet client. I just need to send a telnet request to a local IP address on a LAN issue a "c" then a "b" and stream back the text for internal use. The "c" changes sub-menus and the "b" is a switch to dump the status of a firewall. I need to issue...
2
5407
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 prompt. the problem starts with the password prompt. After I send the password the server returns three nulls '0 0 0' and the thread is closed. can someone help me understand what is going on.
2
5178
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 destinations. must provide login security before make communication 2 telnet communication should not be a direct 1 to 1 communication and that must be as follows Web telnet client -à Web server (telnet client) -à destination (see figure 01)
0
8465
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8895
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8809
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8588
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6210
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4206
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2797
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2032
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1788
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.