reading from sockets
Hello,
I'm trying to read data from a socket and I'm not seeing what I'm
expecting it seems to skip the first line of data. I am new to
Python and just trying to test what I can do with it... and it's not
looking pretty.
I have some Python code:
[--
#! /usr/bin/python
import socket
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.connect( ("localhost",54321) );
s.send("hello")
data = s.recv(1024)
while len(data) > 0:
print repr(data) # the first print of data always seems to "ignore"
the first line of data...
data = s.recv(1024)
--]
On localhost 54321 I have a server (in Java) writing out
[--
first
second
third
--]
(on EOF, the stream is simply closed.)
The result of the script is to print
[--
second
third
--]
I have tried already
[++
data = " "
while len(data) > 0:
data = s.recv(1024)
print repr(data)
++]
but that has not changed anything. The first line to come through is
always skipped it would seem.
Any ideas as to why that would be? I was thinking scope when I made
the last example, but apparently that is not the case.
Andrew
--
http://mail.python.org/mailman/listinfo/python-list
Re: reading from sockets
> I'm assuming that your server waits to receive the word 'hello' before > replying with the three strings (first, second, and third)? So once your Nope - actually it's a threaded "server", with the main thread simply dumping network input to the console and command line input being directly dumped to the network output stream. I confess to having typed the three lines manually...! It's at: http://www.dcs.st-and.ac.uk/~atk1/perso/java/servers/RespondingServer.java It launches on the command line with java RespondingServer _port_ _port_ being the port number it should listen for data on. -- http://mail.python.org/mailman/listinfo/python-list
Re: reading from sockets
Follow up the actual python code is at http://www.dcs.st-and.ac.uk/~atk1/singleclient.py -- http://mail.python.org/mailman/listinfo/python-list
Re: reading from sockets
Simon Forman wrote: > So I'm guessing it's something wrong in your java server. Thanks then. I'll keep testing then... -- http://mail.python.org/mailman/listinfo/python-list
Re: reading from sockets
Simon Forman wrote: > So I'm guessing it's something wrong in your java server. Thanks then. I'll keep testing then... Although I don't seem to have netcat on my unit... I'm using a uni computer so I can't install stuff... but I'm guessing what I wrote is something like a basic-basic thingy that does what netcat is designed to do.? -- http://mail.python.org/mailman/listinfo/python-list
Getting the result of a process after exec*()
Hi, I am trying to write a Python script that takes a ZIP file from a web form (using CGI) and uses either of the UN*X unzip, gunzip, tar, bunzip2 utilities to expand it. I can use Python to save the script to disk; but processing it is another matter. If for example I have received a *.tar.gz file, I need to first pass it through gunzip; then through the tar utility. I also want to process the resulting directory. The problem for me is this: once an external process is called via exec*() the script has effectively fulfilled its task. Is there any way one can process a file with an external process and continue further processing in Python; /once the external processing is completed/? Many thanks, Andrew -- http://mail.python.org/mailman/listinfo/python-list
