[Tutor] Socket Programming issue
Mark Tolonen
metolone+gmane at gmail.com
Wed Jun 22 04:41:24 CEST 2011
"aditya" <nauty.me04 at gmail.com> wrote in message
news:BANLkTikS+gzm=89tHCZpbWKSd+wUfeC7mA at mail.gmail.com...
> This is a small client-server program in which i am using a Vbscript
> program
> to check for connectivity of 2 machines and write the output to a text
> file
> whether it connectes or not ,
>
>
> for example the contents of the file *output.txt* are
>
> 192.168.1.2 is connected
> 192.168.1.10 is not connected
>
>
> Now i am trying to send the contents of this file from a client through a
> socket to a server which is running on my main server .This is basically
> created to automate the checking of connectivity of the machines. The
> issue
> is that although the Vbscript writes the output to the file correctly but
> when the client sends the contents of the file to the server via socket ,
> it
> sometimes prints all the lines of the file on the server and sometimes it
> doesnt , i dont know whether the issue is with the client or the server ,
> Please Help..
>
>
> CLIENT.PY
>
> import socket
> import os
> import sys
> os.system("C:\Python26\ping.vbs")
> host = "localhost"
> port= 23333
> size = 1024
>
> s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
> s.connect((host,port))
>
>
>
>
> f=open('output.txt','r')
> while 1:
> data = f.readline()
> if data: s.send(data)
> else: break
Use "s.sendall(data)" instead of send to correct the error Alan pointed out.
>
>
>
> f.close()
>
> s.close()
>
>
>
> SERVER.PY
>
>
> import socket
>
> host = ""
> port = 23333
> size = 1024
> backlog=5
> s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)
>
> s.bind((host,port))
> s.listen(backlog)
>
s.accept() should be before the while loop, then loop on recv until the
client closes the connection. Since TCP is a streaming protocol, recv()
could receive less than a line or more than one line at once, but with the
accept inside the loop the script will pause waiting for a different client
to connect, and won't continue reading the data from the original client.
-Mark
> while 1:
> client, addr =s.accept()
> data=client.recv(size)
> if data:
> print data
>
> else:
> print "client exit"
>
> s.close()
>
>
>
>
>
>
>
>
>
> --
> Regards
> Aditya
>
--------------------------------------------------------------------------------
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
More information about the Tutor
mailing list