socket programming

2005-07-19 Thread Helge Aksdal
i've recently made my very first socket program in python,
however i've stumbled upon a problem.

this program connects to a server serveral hundred time while it's
executed (it's not possible to let connection stay up, because the
server closes it), and after a time my program dies with the error:
"socket.error: (134, 'Transport endpoint is not connected')"

if i then change to a console window, and telnet to this server it
sends me to another one. That's probably why my program dies, how
can i get my code to handle this?

Trying xxx.xxx.xxx.xxx
telnet: connect to address xxx.xxx.xxx.xxx: Connection refused
Trying xxx.xxx.xxx.xxx
Connected to xx.x.
Escape character is '^]'.

here is my current connection code:

def connect(self, server, port):
self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.sock.setblocking(0)
self.sock.connect((server, port))
(sread, swrite, serror) = select.select([], [self.sock], [], 10)
if swrite.count(self.sock) > 0:
i = self.sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
if 0 == i:
self.sock.setblocking(1)

-- 
Helge Aksdal
-- 
http://mail.python.org/mailman/listinfo/python-list


tcp socket programming

2005-10-04 Thread Mohammed Smadi
hi;
If i have a tcp connection with a remote server, what is a good way to 
read all the data into a buffer before starting to process the data?
I know that the data recieved will be 3 lines with CRLF between them.  
However if I can sock.recv(1024) the output is not consistent all the 
time, sometime i get one line and sometimes i get two.  So I figures I 
should read all the data first then work on it and I used the following 
code:
result = []
while True:
got=s.recv(1024)
print got
if not got: break
result.append(got)
got = [] # i tried also taking this out
s.close()

but this code just hangs in the loop and never quits

any ideas will be much appreciated

moe smadi

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-19 Thread Jaime Wyant
It sounds really strange to connect to a server "several hundred"
times.  If I had to guess, the server monitored all of the connects
coming from your IP address and eventually stopped accepting ANY
connections.

jw

On 7/19/05, Helge Aksdal <[EMAIL PROTECTED]> wrote:
> i've recently made my very first socket program in python,
> however i've stumbled upon a problem.
> 
> this program connects to a server serveral hundred time while it's
> executed (it's not possible to let connection stay up, because the
> server closes it), and after a time my program dies with the error:
> "socket.error: (134, 'Transport endpoint is not connected')"
> 
> if i then change to a console window, and telnet to this server it
> sends me to another one. That's probably why my program dies, how
> can i get my code to handle this?
> 
> Trying xxx.xxx.xxx.xxx
> telnet: connect to address xxx.xxx.xxx.xxx: Connection refused
> Trying xxx.xxx.xxx.xxx
> Connected to xx.x.
> Escape character is '^]'.
> 
> here is my current connection code:
> 
> def connect(self, server, port):
> self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> self.sock.setblocking(0)
> self.sock.connect((server, port))
> (sread, swrite, serror) = select.select([], [self.sock], [], 10)
> if swrite.count(self.sock) > 0:
> i = self.sock.getsockopt(socket.SOL_SOCKET, socket.SO_ERROR)
> if 0 == i:
> self.sock.setblocking(1)
> 
> --
> Helge Aksdal
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-19 Thread Helge Aksdal
* Jaime Wyant <[EMAIL PROTECTED]> [2005/07/19 21:26]:

> It sounds really strange to connect to a server "several hundred"
> times.  If I had to guess, the server monitored all of the connects
> coming from your IP address and eventually stopped accepting ANY
> connections.

That's really what it eventually does, just that it refuses my
connection, and a connection to another server is established.

The alternative is to do it by http connection, but i'm afraid
that's going to be too slow. or am i wrong here?

To help you understand me better, this is a server at work that
gives me some account information, so when i want to check "several
hundred" accounts i need to look them up one by one since the server
closes the connection after each query.

-- 
Helge Aksdal
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-19 Thread Terry Reedy

"Helge Aksdal" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>* Jaime Wyant <[EMAIL PROTECTED]> [2005/07/19 21:26]:
>
>> It sounds really strange to connect to a server "several hundred"
>> times.  If I had to guess, the server monitored all of the connects
>> coming from your IP address and eventually stopped accepting ANY
>> connections.
>
> That's really what it eventually does, just that it refuses my
> connection, and a connection to another server is established.
>
> The alternative is to do it by http connection, but i'm afraid
> that's going to be too slow. or am i wrong here?
>
> To help you understand me better, this is a server at work that
> gives me some account information, so when i want to check "several
> hundred" accounts i need to look them up one by one since the server
> closes the connection after each query.

Ask your server administrator if you are bumping up against a hidden 
connection limit.  Or if a query can ask about multiple accounts.

tjr



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-19 Thread Helge Aksdal
* Terry Reedy <[EMAIL PROTECTED]> [2005/07/19 22:57]:

> Ask your server administrator if you are bumping up against a hidden 
> connection limit.  Or if a query can ask about multiple accounts.

how can i use a connection limit to my advantage?
i know for certain that a query can't ask about multiple accounts.

-- 
Helge Aksdal
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-19 Thread Jaime Wyant
On 7/19/05, Helge Aksdal <[EMAIL PROTECTED]> wrote:
> * Terry Reedy <[EMAIL PROTECTED]> [2005/07/19 22:57]:
> 
> > Ask your server administrator if you are bumping up against a hidden
> > connection limit.  Or if a query can ask about multiple accounts.
> 
> how can i use a connection limit to my advantage?
> i know for certain that a query can't ask about multiple accounts.
> 

I don't think you can use a limit to your advantage.  If you are
bumping against some limit and you can't query for multiple accounts
then there doesn't seem to be much you can really do.

jw
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-19 Thread Helge Aksdal
* Jaime Wyant <[EMAIL PROTECTED]> [2005/07/19 23:09]:

> I don't think you can use a limit to your advantage.  If you are
> bumping against some limit and you can't query for multiple accounts
> then there doesn't seem to be much you can really do.

The problem would be solved, if i just could get my socket code to
follow the redirection, as i tried to explain in my first email.

any tips? or do i have to use good old nasty time.sleep to avoid this?

-- 
Helge Aksdal
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-19 Thread Grant Edwards
On 2005-07-19, Helge Aksdal <[EMAIL PROTECTED]> wrote:

> if i then change to a console window, and telnet to this
> server it sends me to another one.

What do you mean "sends me to another one"?

The telnet protocol doesn't have any sort of "redirect"
capability.

> That's probably why my program dies, how can i get my code to
> handle this?

I don't understand what "this" is.  If the server is sending
you some sort of redirect message, then parse it and obey it.

-- 
Grant Edwards   grante Yow!  I'm RELIGIOUS!! I
  at   love a man with a
   visi.comHAIRPIECE!! Equip me with
   MISSILES!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-19 Thread John Hazen
* Helge Aksdal <[EMAIL PROTECTED]> [2005-07-19 11:23]:
> if i then change to a console window, and telnet to this server it
> sends me to another one. That's probably why my program dies, how
> can i get my code to handle this?
> 
> Trying xxx.xxx.xxx.xxx
> telnet: connect to address xxx.xxx.xxx.xxx: Connection refused
> Trying xxx.xxx.xxx.xxx
> Connected to xx.x.
> Escape character is '^]'.

I don't think the server is "sending you to another one".  I think
perhaps DNS returns two IP addresses for the name, and after the first
one fails, your telnet client tries the second?

I'm just guessing, since you didn't include what command you ran to do
the telnet.  As Grant mentioned, the telnet protocol doesn't do
redirects.

-John
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-19 Thread Terry Reedy

"Helge Aksdal" <[EMAIL PROTECTED]> wrote in message 
news:[EMAIL PROTECTED]
>* Terry Reedy <[EMAIL PROTECTED]> [2005/07/19 22:57]:
>
>> Ask your server administrator if you are bumping up against a hidden
>> connection limit.  Or if a query can ask about multiple accounts.
>
> how can i use a connection limit to my advantage?

I was suggesting acquisition of knowledge.  If you are making inquiries as 
part of a work assignment by the company that is restricting your 
inquiries, then you have an explanation for why you cannot complete the 
project as soon as expected.

Terry J. Reedy



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-20 Thread gry
What I have done in similar circumstances is put in a random sleep
between connections to fool the server's load manager.  Something like:

.import time
.min_pause,max_pause = (5.0, 10.0) #seconds
.while True:
.   time.sleep(random.uniform(min_pause, max_pause))
.   do_connection_and_query_stuff()

It works for me.  Just play with the pause parameters until it fails
and add a little.

-- George

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-20 Thread Helge Aksdal
* gry@ll.mit.edu  [2005/07/20 15:26]:

> What I have done in similar circumstances is put in a random sleep
> between connections to fool the server's load manager.  Something like:
> 
> .import time
> .min_pause,max_pause = (5.0, 10.0) #seconds
> .while True:
> .   time.sleep(random.uniform(min_pause, max_pause))
> .   do_connection_and_query_stuff()
> 
> It works for me.  Just play with the pause parameters until it fails
> and add a little.

thanks for the tip. i'll give that a shot.

-- 
Helge Aksdal
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming

2005-07-21 Thread Helge Aksdal

* gry@ll.mit.edu  [2005/07/20 15:26]:

> What I have done in similar circumstances is put in a random sleep
> between connections to fool the server's load manager.  Something like:
> 
> .import time
> .min_pause,max_pause = (5.0, 10.0) #seconds
> .while True:
> .   time.sleep(random.uniform(min_pause, max_pause))
> .   do_connection_and_query_stuff()
> 
> It works for me.  Just play with the pause parameters until it fails
> and add a little.

thanks, this worked for me too. slows down the program, but at least
it works. :)

-- 
Helge Aksdal
-- 
http://mail.python.org/mailman/listinfo/python-list


Socket Programming - Question

2006-02-10 Thread [EMAIL PROTECTED]
I am relatively new to Python, and wanted to see if this is even
possible, and if so how to go about implementing it.  What I'm looking
to do is create a client/server application that does the following:

1)  System2 listens on port > 1023
2)  System1 connects to System2 and sends traffic to it - based on the
traffic it receives (i.e. a special string), System2 executes
command-line commands and returns the output to System1.

An example of what I am looking to use this for is for remote virus
scanning.  So System2 listens, System1 connects and sends it the
traffic to start the scan, then System1 returns either what would be
listed in the Command Prompt window, or possibly the contents of the
logfile.

Any help would be greatly appreciated!

Doug

-- 
http://mail.python.org/mailman/listinfo/python-list


socket programming question

2006-06-20 Thread Kiran
Hello All,
  My question is, is it possible to make python do some other
processing while it is waiting for a socket to timeout?

thanks a lot!
Kiran

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tcp socket programming

2005-10-04 Thread Irmen de Jong
Mohammed Smadi wrote:
> hi;
> If i have a tcp connection with a remote server, what is a good way to 
> read all the data into a buffer before starting to process the data?
> I know that the data recieved will be 3 lines with CRLF between them.  
> However if I can sock.recv(1024) the output is not consistent all the 
> time, sometime i get one line and sometimes i get two.  So I figures I 
> should read all the data first then work on it and I used the following 
> code:
> result = []
> while True:
> got=s.recv(1024)
> print got
> if not got: break
> result.append(got)
> got = [] # i tried also taking this out
> s.close()
> 
> but this code just hangs in the loop and never quits

... because it doesn't 'know' when to stop reading.
The socket recv() returns anything from 0 to 1024 bytes
depending on the amount of data that is available at that time.

You have to design your wire protocol a bit differently if you want
to do this in a consistent, reliable way.
For instance, you can decide on sending *one* byte first that
signifies the amount of bytes to read after that. (limiting the
size to 255 ofcourse).

Or you will have to change your read-loop to read until it
encountered the third CRLF occurrence (and no more!)

The latter is actually quite easily done by not reading directly
from the socket object, but first converting it to a file-like
object:

s=socket.socket()
s.connect(...)

fs=s.makefile()
fs.readline()
fs.readline()
fs.readline()


--Irmen.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: tcp socket programming

2005-10-04 Thread Mohammed Smadi

On Tue, 4 Oct 2005, Irmen de Jong wrote:

> Mohammed Smadi wrote:
> > hi;
> > If i have a tcp connection with a remote server, what is a good way to 
> > read all the data into a buffer before starting to process the data?
> > I know that the data recieved will be 3 lines with CRLF between them.  
> > However if I can sock.recv(1024) the output is not consistent all the 
> > time, sometime i get one line and sometimes i get two.  So I figures I 
> > should read all the data first then work on it and I used the following 
> > code:
> > result = []
> > while True:
> > got=s.recv(1024)
> > print got
> > if not got: break
> > result.append(got)
> > got = [] # i tried also taking this out
> > s.close()
> > 
> > but this code just hangs in the loop and never quits
> 
> ... because it doesn't 'know' when to stop reading.
> The socket recv() returns anything from 0 to 1024 bytes
> depending on the amount of data that is available at that time.
> 
> You have to design your wire protocol a bit differently if you want
> to do this in a consistent, reliable way.
> For instance, you can decide on sending *one* byte first that
> signifies the amount of bytes to read after that. (limiting the
> size to 255 ofcourse).
> 
> Or you will have to change your read-loop to read until it
> encountered the third CRLF occurrence (and no more!)
> 
> The latter is actually quite easily done by not reading directly
> from the socket object, but first converting it to a file-like
> object:
> 
> s=socket.socket()
> s.connect(...)
> 
> fs=s.makefile()
> fs.readline()
> fs.readline()
> fs.readline()
> 
> 
> --Irmen.
> 
that seems to have worked.  I need to do more testing.  Thanks

-- 
http://mail.python.org/mailman/listinfo/python-list


Socket programming design problem

2005-12-22 Thread David
After programming with Python for a few hours, I've come up with some code:
http://p.shurl.net/3n.  However, now I've realised there's a bit of a
problem with the original design.

I have a number of questions, if anybody could answer I'd be grateful.

a) Big problem, I can't see how to receive from more than one socket at
once.  I need to do this so that data from the TCP connection can be sent
out on the UDP one and vice versa.  Do I need a thread for each, or is
there some other way I can listen on two sockets at once (maybe
non-blocking ones?)

b) If the script dies prematurely, e.g. I mistyped something and Python
throws an exception, it is caught by the generic handler at the bottom. 
However, the socket does not seem to be cleaned up properly, as if I try to
run it again immediately I get an error that it is still in use.  lsof
doesn't show it in use though, which seems weird.  Is there any particular
reason for this?

Thanks,

David
-- 
http://mail.python.org/mailman/listinfo/python-list


Socket Programming HOWTO example

2006-01-16 Thread Marco Meoni
Hi. I read the Gordon McMillan's "Socket Programming Howto".
I tried to use the example in this howto but this doesn't work.
The code is class mysocket:
'''classe solamente dimostrativa
  - codificata per chiarezza, non per efficenza'''
def __init__(self, sock=None):
if sock is None:
self.sock = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
else:
self.sock = sock
def connect(host, port):
self.sock.connect((host, port))
def mysend(msg):
totalsent = 0
while totalsent < MSGLEN:
sent = self.sock.send(msg[totalsent:])
if sent == 0:
raise RuntimeError, \\
"connessione socket interrotta"
totalsent = totalsent + sent
def myreceive():
msg = ''
while len(msg) < MSGLEN:
chunk = self.sock.recv(MSGLEN-len(msg))
if chunk == '':
raise RuntimeError, \\
"connessione socket interrotta"
msg = msg + chunk
return msg
 
How can i use this?
Thanks all!
Marco

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket Programming - Question

2006-02-10 Thread [EMAIL PROTECTED]
[EMAIL PROTECTED] wrote:

> An example of what I am looking to use this for is for remote virus
> scanning.  So System2 listens, System1 connects and sends it the

Just found this through OSNews:
http://rpyc.sourceforge.net/

It actually seems to be a perfect fit for your job.

Lorenzo

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket Programming - Question

2006-02-10 Thread Grant Edwards
On 2006-02-11, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:

> I am relatively new to Python, and wanted to see if this is
> even possible, and if so how to go about implementing it.
> What I'm looking to do is create a client/server application
> that does the following:
>
> 1)  System2 listens on port > 1023
> 2)  System1 connects to System2 and sends traffic to it - based on the
> traffic it receives (i.e. a special string), System2 executes
> command-line commands and returns the output to System1.

Sure.  Just use os.popen() or one of it's relatives to execute
the program:

http://www.python.org/doc/current/lib/os-newstreams.html#os-newstreams

-- 
Grant Edwards   grante Yow!  I'm not available
  at   for comment...
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket Programming - Question

2006-02-10 Thread D
Thanks!  Now, I'm a bit confused as to exactly how it works - will it
display the output of what it executes on the target system?  I would
like to create a window in Tktinker to where a user can select options
(such as run scan on remote system) - it would then run the
command-line based scan and return the output.  Does this sound like
something it would do?  Thanks again :)

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket Programming - Question

2006-02-10 Thread D
I've used os.popen() before, but if I execute it on a remote system how
could I get the output back to the requesting machine?

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket Programming - Question

2006-02-10 Thread Paul Rubin
"[EMAIL PROTECTED]" <[EMAIL PROTECTED]> writes:
> I am relatively new to Python, and wanted to see if this is even
> possible, and if so how to go about implementing it.  What I'm looking
> to do is create a client/server application that does the following:
> 
> 1)  System2 listens on port > 1023
> 2)  System1 connects to System2 and sends traffic to it - based on the
> traffic it receives (i.e. a special string), System2 executes
> command-line commands and returns the output to System1.

You're asking how to write a TCP server in general.  You might look at
the SocketServer module in the standard library, which gives a
reasonable framework for that kind of server.  However, its
documentation is not very good.  Alex Martelli's "Python Cookbook" may
have some better examples.  

If you want your server to be able to handle multiple client sessions
simultaneously, use SocketServer.ThreadingMixin (for multiple threads)
or SocketServer.ForkingMixin (multiple processes).  Beware that this
stuff is not easy for beginners, unless you've had experience writing
servers in other languages (maybe Java).

There's another issue too, especially if your app is a virus scanner:
you have to think very hard about what happens if a malicious client
connects to your server (a virus scanning app is an unusually juicy
target for such attacks).  It's extremely easy to leave security holes
open (the viruses themselves typically exploit such holes in Windows)
so you have to develop a paranoid attitude about what kinds of things
the attacker can try and how you can defend.  Using Python puts you
one step ahead of Windows, since you're mostly immune to buffer
overflow bugs, a very common vulnerability.  But it's still an area
full of hazards and not so good for beginners.

This is good bedtime reading: http://www.dwheeler.com/secure-programs/
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket Programming - Question

2006-02-10 Thread Grant Edwards
On 2006-02-11, D <[EMAIL PROTECTED]> wrote:

> I've used os.popen() before, but if I execute it on a remote
> system how could I get the output back to the requesting
> machine?

Write it to the socket?

-- 
Grant Edwards   grante Yow!  Where does it go when
  at   you flush?
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming question

2006-06-20 Thread Irmen de Jong
Kiran wrote:
> Hello All,
>   My question is, is it possible to make python do some other
> processing while it is waiting for a socket to timeout?

sure, you have to use threads and/or use asynchronous socket 
programming. Google is your friend.

--Irmen
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming question

2006-06-20 Thread Grant Edwards
On 2006-06-20, Kiran <[EMAIL PROTECTED]> wrote:

> is it possible to make python do some other processing while
> it is waiting for a socket to timeout?

Yes.

You can either use threads or select.

Or you can use one of the async server frameworks like twisted.

-- 
Grant Edwards   grante Yow!  I'd like MY data-base
  at   JULIENNED and stir-fried!
   visi.com
-- 
http://mail.python.org/mailman/listinfo/python-list


socket programming and port scans

2005-05-18 Thread rbt
I don't fully understand sockets, I just know enough to be dangerous. 
The below is not detected by nmap, but is affected by iptables or ipsec. 
Can anyone explain why that is?

 s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
 s.bind((ip_param, port_param))
 while 1:
 s.listen(5)

I can connect to the socket server and even transmit data so I know it's 
there.

Thanks,
rbt
-- 
http://mail.python.org/mailman/listinfo/python-list


Send all packets (socket programming)

2005-05-22 Thread Tomas Christiansen
Is there a way to make shure that all packets has been sent before issuing 
a Shutdown and/or Close on a (TCP) Socket?

The problem is that "the other end" interprets a reset-flag (RST) on an IP-
packet as an indication of an error. Instead the finish-flag (FIN) must be 
set, in order to close the connection non-errornously. That can be achieved 
by using socket.shutdown(SHUT_RDWR) followed by socket.close().

BUT if all data has not been sent, sometimes shutdown(SHUT_RDWR) followed 
by close(), sets RST, and the other end thinks that something went wrong 
(the other end is some unknown TCP/IP printer) and goes to an error-state.

---
Tomas
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket programming design problem

2005-12-22 Thread John J. Lee
David <[EMAIL PROTECTED]> writes:
[...]
> a) Big problem, I can't see how to receive from more than one socket at
> once.  I need to do this so that data from the TCP connection can be sent
> out on the UDP one and vice versa.  Do I need a thread for each, or is
> there some other way I can listen on two sockets at once (maybe
> non-blocking ones?)

1. threads, yes
2. http://docs.python.org/lib/module-select.html (if on non-Windows system)
3. http://docs.python.org/lib/module-asyncore.html (ditto)
4. http://twistedmatrix.com/


Interesting but maybe not production-ready code:

1. http://kamaelia.sourceforge.net/Home
2. http://poshmodule.sourceforge.net/


> b) If the script dies prematurely, e.g. I mistyped something and Python
> throws an exception, it is caught by the generic handler at the bottom. 
> However, the socket does not seem to be cleaned up properly, as if I try to
> run it again immediately I get an error that it is still in use.  lsof
> doesn't show it in use though, which seems weird.  Is there any particular
> reason for this?

Google for SO_REUSEADDR



John

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket programming design problem

2005-12-22 Thread Dan Sommers
On Thu, 22 Dec 2005 22:12:09 +,
David <[EMAIL PROTECTED]> wrote:

> a) Big problem, I can't see how to receive from more than one socket
> at once.  I need to do this so that data from the TCP connection can
> be sent out on the UDP one and vice versa.  Do I need a thread for
> each, or is there some other way I can listen on two sockets at once
> (maybe non-blocking ones?)

Try the select module:

http://www.python.org/doc/current/lib/module-select.html

Regards,
Dan

-- 
Dan Sommers

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket Programming HOWTO example

2006-01-16 Thread Steve Holden
Marco Meoni wrote:
> Hi. I read the Gordon McMillan's "Socket Programming Howto".
> I tried to use the example in this howto but this doesn't work.
> The code is class mysocket:
> '''classe solamente dimostrativa
>   - codificata per chiarezza, non per efficenza'''
> def __init__(self, sock=None):
> if sock is None:
> self.sock = socket.socket(
> socket.AF_INET, socket.SOCK_STREAM)
> else:
> self.sock = sock
> def connect(host, port):
> self.sock.connect((host, port))
> def mysend(msg):
> totalsent = 0
> while totalsent < MSGLEN:
> sent = self.sock.send(msg[totalsent:])
> if sent == 0:
> raise RuntimeError, \\
> "connessione socket interrotta"
> totalsent = totalsent + sent
> def myreceive():
> msg = ''
> while len(msg) < MSGLEN:
> chunk = self.sock.recv(MSGLEN-len(msg))
> if chunk == '':
> raise RuntimeError, \\
> "connessione socket interrotta"
> msg = msg + chunk
> return msg
>  
> How can i use this?

Well, a lot depends on what you mean by "doesn't work".

I can see you have changed the example a little (because I know that 
Gordon's original didn't have comments in Italian). Did the program 
produce a syntax error, a traceback or what? It would have been helpful 
if you had included a link to Gordon's tutorial -- I presume you mean 
the one at

   http://www.amk.ca/python/howto/sockets/

What are you trying to do, and why do you think this particular chunk of 
code might help you? Did you actually try to create and use any 
instances of this class?

regards
  Steve
-- 
Steve Holden   +44 150 684 7255  +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006  www.python.org/pycon/

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket Programming HOWTO example

2006-01-17 Thread Manlio Perillo
Steve Holden ha scritto:
> [...]
> I can see you have changed the example a little (because I know that
> Gordon's original didn't have comments in Italian). 

The example cames from italian translation of the howto:
http://python.it/doc/howto/Socket/sockets-it/sockets-it.html




Regards  Manlio Perillo
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket Programming HOWTO example

2006-01-17 Thread Bryan Olson
Marco Meoni wrote:
> Hi. I read the Gordon McMillan's "Socket Programming Howto".
> I tried to use the example in this howto but this doesn't work.

You are right, that obviously won't work. The code passes
'self' to __init__, but not to any of the others methods.

I'm cc'ing this post to [EMAIL PROTECTED]


> The code is 
 > class mysocket:
> '''classe solamente dimostrativa
>   - codificata per chiarezza, non per efficenza'''
> def __init__(self, sock=None):
> if sock is None:
> self.sock = socket.socket(
> socket.AF_INET, socket.SOCK_STREAM)
> else:
> self.sock = sock
> def connect(host, port):
> self.sock.connect((host, port))
> def mysend(msg):
> totalsent = 0
> while totalsent < MSGLEN:
> sent = self.sock.send(msg[totalsent:])
> if sent == 0:
> raise RuntimeError, \\
> "connessione socket interrotta"
> totalsent = totalsent + sent

To send exactly MSGLEN bytes, use socket's 'sendall' method.

> def myreceive():
> msg = ''
> while len(msg) < MSGLEN:
> chunk = self.sock.recv(MSGLEN-len(msg))
> if chunk == '':
> raise RuntimeError, \\
> "connessione socket interrotta"
> msg = msg + chunk
> return msg

> How can i use this?

Treat it as a "HowNotTo".


-- 
--Bryan
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Socket Programming HOWTO example

2006-01-17 Thread Bryan Olson
I mis-phrased:
> The code passes
> 'self' to __init__, but not to any of the others methods.

Of course I meant that the formal parameter for self is missing.

>  > class mysocket:
> 
>> '''classe solamente dimostrativa
>>   - codificata per chiarezza, non per efficenza'''
>> def __init__(self, sock=None):
>> if sock is None:
>> self.sock = socket.socket(
>> socket.AF_INET, socket.SOCK_STREAM)
>> else:
>> self.sock = sock
>> def connect(host, port):
>> self.sock.connect((host, port))
>> def mysend(msg):
[...]
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming and port scans

2005-05-18 Thread millerch
Are you explicitly referencing the port for nmap, or is it a general 
port scan?

The version of nmap I run only checks common ports unless a port range 
is specified.

rbt wrote:
> I don't fully understand sockets, I just know enough to be dangerous. 
> The below is not detected by nmap, but is affected by iptables or ipsec. 
> Can anyone explain why that is?
> 
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.bind((ip_param, port_param))
> while 1:
> s.listen(5)
> 
> I can connect to the socket server and even transmit data so I know it's 
> there.
> 
> Thanks,
> rbt
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming and port scans

2005-05-18 Thread rbt
millerch wrote:
> Are you explicitly referencing the port for nmap, or is it a general 
> port scan?
> 
> The version of nmap I run only checks common ports unless a port range 
> is specified.

I did not know that. It works as expected now. Thanks!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: socket programming and port scans

2005-05-19 Thread Peter Hansen
rbt wrote:
> I don't fully understand sockets, I just know enough to be dangerous. 
> The below is not detected by nmap, but is affected by iptables or ipsec. 
> Can anyone explain why that is?
> 
> s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.bind((ip_param, port_param))
> while 1:
> s.listen(5)

This, by the way, is wrong.  s.listen(5) just tells the stack that you 
would like to allow a backlog of up to 5 waiting-to-be-connected 
connection attempts while you are accepting another.  The call doesn't 
block and needn't be called repeatedly.  You could just as well do the 
call to listen first, then have an empty "while 1: pass" loop (but note 
that in either case it is a "busy wait", consuming 100% CPU while it runs).

What you are looking for is more like this:

s.listen(5)
while 1:
 s.accept()  # wait for connection, and ignore it


If you want to simulate a little server to allow multiple connections, 
you would of course need to use the value returned by accept() and 
probably call close() on the client socket right away.

-Peter
-- 
http://mail.python.org/mailman/listinfo/python-list


Hi, about socket programming and threading

2006-01-17 Thread Kr z
Hi All!I wonder if anyone knows the simple code structure for a multithreaded web server handling multiple clients at the same time?Thanx!Regards.KrzGet an advanced look at the new version of  MSN Messenger. 

-- 
http://mail.python.org/mailman/listinfo/python-list