tcp socket programming
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: tcp socket programming
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
closing sockets
hi; i am executing program whcih uses a tcp socket. At the end of the program i do s.close() where s is my socket. when i try to run the program again right away i get the following error Traceback (most recent call last): File "asterisk_login.py", line 14, in ? s.bind(("", hp_port)) # do some error checking File "", line 1, in bind socket.error: (98, 'Address already in use') how can i get around that thanks smadi -- http://mail.python.org/mailman/listinfo/python-list
how to get any available port
hi; if am using s.bind for a tcp socket. On the client side i dont really care which socket i use as long as i get an available socket. Is there a funciton or a way to get an available socket? thanks smadi -- http://mail.python.org/mailman/listinfo/python-list
Re: how to get any available port
On Tue, 4 Oct 2005, Grant Edwards wrote: > On 2005-10-04, ncf <[EMAIL PROTECTED]> wrote: > > > Hmm...perhaps he is trying to do a transfer thing like many chat > > programs do. Instead of sending large files across a server, you > > "Direct Connect" and send the file directly. :shrugs: > > So how does that require binding the client end of a TCP > connection? > > what else would you do? I am using examples from the web and they all bind to a port at the localhost before connecting to the remote host. my code is like this #transmission socket s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) s.bind(("",hp_port)) # do some error checking data="HI" print data s.connect(('192.168.2.13',port)) s.send(data) print "\n" which is working fine for me after putting the s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) statement any suggestions for alternative implementation? moe smadi -- http://mail.python.org/mailman/listinfo/python-list
calling matlab
Hi; Does anyone know if we can call matlab for a python or bash script while feeding the matlab script some command line arguments? I have an interactive matlab script which i want to automate by feeding the args from a script. I know this is probably more suitable to a matlab group but any ideas will be appreciated. thanks moe smadi -- http://mail.python.org/mailman/listinfo/python-list
pyNMS
hi anybody used pyNMS b4? I compiled it and seem to be able to import modules but cannot use em, or more like cannot call them since if i do something like: import ping ping.ttl(..) i get an error like Traceback (most recent call last): File "", line 1, in ? AttributeError: 'module' object has no attribute 'ttl' any clue? thanks smadi -- http://mail.python.org/mailman/listinfo/python-list
socket.gethostbyaddr problem
hi; I am trying to do some very basic socket programming and i get the following error. Any help will be appreciated: Code: import socket x = socket.gethostbyaddr("www.google.ca") return an error: socket.herror: (1, 'Unknown host') I tried replacing the web URL with an IP address on the net, on my LAN and it does not work. It only works with "localhost" which is not very useful. Am using Python 2.3.4 if that matters. any direction will be great moe smadi -- http://mail.python.org/mailman/listinfo/python-list
sockets question
hi; i have the following piece of code: = s = socket.socket(socket.AF_INET,socket.SOCK_DGRAM) s.bind(("",port)) s.sendto(data,(MY_GW,port)) data = s.recvfrom(1024) data contains some instructions which i am sending to MY_GW. If the reply arrives from the MY_GW quickly then things fine, otherwise the program is going to freeze waiting for the reply. I want to do the following: 1- Be able to impose a timeout after which i want to stop executing the s.recvfrom statement. rgds; m.smadi -- http://mail.python.org/mailman/listinfo/python-list