Re: [Tutor] Socket programming

2015-01-03 Thread Alan Gauld

On 03/01/15 11:48, pramod gowda wrote:


client code:

import socket

client_socket=socket.socket()
server_address='192.168.2.2'
server_port= 80


see below regarding port numbers


print("hello")
client_socket.connect((server_address,server_port))
print("hello")
data=client_socket.recv(1024)
print(data)
client_socket.close()



I assume it is this code that is not doing what you expect?
Try using more descriptive print statements.
Also, when debugging, never use a print that could be blank,
always have a fixed string so you can see it. So instead of

print(data)

use

print('Received: ', data)

That way you can see that data was empty rather than that
the print didn't execute.

I'd also put a print(finished') or similar after closing
the socket.


server code:
import socket

server_socket=socket.socket()
server_name='192.168.2.2'
server_port= 80


port 80 is usually reserved for http traffic, better
to choose a port number greater than 1024 for user
programs.


server_socket.bind((server_name,server_port))
server_socket.listen(1)


I'd up the queue size to at least 3  or 5.
You are not leaving much room for errors.


while True:
 print("hello")
 c,address=server_socket.accept()
 print("we got connection from:",address)


Is this print appearing OK?


 c.send("hello,hw ru")


I'd add a print here too, to verify that the send worked.


 c.close()



I am not getting the output, i am using windows 7 OS,pyhton ver:..
please  check and give me the solution.


It's not clear what you mean by "the output".
Are you getting anything printed? At the client and the server?

It might help to show us a cut n paste from both client
and server.

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.amazon.com/author/alan_gauld
Follow my photo-blog on Flickr at:
http://www.flickr.com/photos/alangauldphotos


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


[Tutor] Socket programming

2015-01-03 Thread pramod gowda
Hi,

 i am learning socket programming,

client code:

import socket

client_socket=socket.socket()
server_address='192.168.2.2'
server_port= 80
print("hello")
client_socket.connect((server_address,server_port))
print("hello")
data=client_socket.recv(1024)
print(data)
client_socket.close()

server code:
import socket

server_socket=socket.socket()
server_name='192.168.2.2'
server_port= 80
server_socket.bind((server_name,server_port))
server_socket.listen(1) 3.4.2

while True:
print("hello")
c,address=server_socket.accept()
print("we got connection from:",address)
c.send("hello,hw ru")
c.close()




I am not getting the output, i am using windows 7 OS,pyhton ver:..
please  check and give me the solution.
Regards,

Pramod SP
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2013-04-08 Thread Lolo Lolo
on socket programming. if as a client or server, the information being sent has 
a buffer size on 2048, will this retrieve all data below 2KB? or can it be 
exceeded with say 2100? or does 2100 fall under 3KB?___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2013-04-08 Thread Alan Gauld

On 08/04/13 08:06, Mousumi Basu wrote:


import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
 s=s.bind(('172.18.2.11',2213))
 print 'socket bind is complete'
except socket.error,msg:
 print 'bind failed'
 sys.exit()


Your first error is that when developing you don't want to hide the 
error messages so add a raise statement to the except block so you get 
the full traceback with all of its helpful details.




but when i am writting;-

s=s.bind(('localhost',2213))

the output is;-
socket bind is complete


Which suggests that the 172... IP address you are using above is not 
recognised... or maybe has a firewall around it, or some other 
restrictions on access.



Can you please tell me where i am making the error?
(The computers are connected as the ping operation is executing properly
in command prompt)


It is most likely a network configuration issue but only you can fix 
that since we can't see how the network is set up. Rather than using 
ping try using telnet to reach the IP address and port and see what 
response you get.


--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2013-04-08 Thread Kushal Kumaran
Mousumi Basu  writes:

> I want to perform binding between two computers having ip addresses
> 172.18.2.11 and  172.18.2.95.So i wrote the following code(on the computer
> having IP address 172.18.2.95):-
>
>
> import socket
> import sys
> s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
> try:
> s=s.bind(('172.18.2.11',2213))

The bind method does not do what you think it does.

To bind to an IP address, the address must be assigned to one of the
interfaces on the host where the code is running.  So your bind call
will not work on 172.18.2.95.  It will only work on 172.18.2.11.  To
make a connection to a different host, you call the connect method.

Since you are using SOCK_DGRAM, an explicit connect call is not
required.  You can just call the sendto method on the socket.  On the
other end, the socket will need the bind method to be called.  It can
then call recvfrom to receive data.

You should read an introductory sockets programming text if you intend
to use the python socket module.
This page: http://beej.us/guide/bgnet/output/html/multipage/index.html
which showed up in my web search, seems simple.  You will have to
translate C code to python, but the basic concepts will apply.

> print 'socket bind is complete'
> except socket.error,msg:
> print 'bind failed'
> sys.exit()
>
>
> The output is :-
> bind failed
>
>
> but when i am writting;-
>
> s=s.bind(('localhost',2213))
>
> the output is;-
> socket bind is complete
>
>
>
> Can you please tell me where i am making the error?
> (The computers are connected as the ping operation is executing properly in
> command prompt)

-- 
regards,
kushal
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2013-04-08 Thread Steven D'Aprano

On 08/04/13 17:06, Mousumi Basu wrote:

I want to perform binding between two computers having ip addresses
172.18.2.11 and  172.18.2.95.So i wrote the following code(on the computer
having IP address 172.18.2.95):-


import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
 s=s.bind(('172.18.2.11',2213))
 print 'socket bind is complete'
except socket.error,msg:
 print 'bind failed'
 sys.exit()




Why are you throwing away the useful information Python gives you to debug the
problem? That's like trying to find your lost keys by deliberately putting on a
blindfold.

Never, never, never catch an exception only to print a useless message that
tells you nothing. "Bind failed"? Why did it fail? Who knows! You threw that
information away. You might as well have said "An error occurred".

Instead, use this code:


import socket
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
s = s.bind(('172.18.2.11', 2213))
print 'socket bind is complete'


If an error occurs, Python will automatically print a full traceback showing
you exactly:

* what went wrong
* where it went wrong
* what line of code caused the problem
* what type of problem it was

instead of just a useless "an error occurred" message.


Once you have a proper traceback, and you can see the actual error message,
then and only then can we begin to solve the problem.




--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2013-04-08 Thread Timo

Op 08-04-13 09:06, Mousumi Basu schreef:
I want to perform binding between two computers having ip addresses  
172.18.2.11 and 172.18.2.95.So  i wrote the 
following code(on the computer having IP address 172.18.2.95):-



import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
s=s.bind(('172.18.2.11',2213))
print 'socket bind is complete'
except socket.error,msg:
print 'bind failed'
sys.exit()

You are hiding the error message here. Just print msg and see why it fails.

Timo




The output is :-
bind failed


but when i am writting;-

s=s.bind(('localhost',2213))

the output is;-
socket bind is complete



Can you please tell me where i am making the error?
(The computers are connected as the ping operation is executing 
properly in command prompt)








___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Socket Programming

2013-04-08 Thread Mousumi Basu
I want to perform binding between two computers having ip addresses
172.18.2.11 and  172.18.2.95.So i wrote the following code(on the computer
having IP address 172.18.2.95):-


import socket
import sys
s=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
s=s.bind(('172.18.2.11',2213))
print 'socket bind is complete'
except socket.error,msg:
print 'bind failed'
sys.exit()


The output is :-
bind failed


but when i am writting;-

s=s.bind(('localhost',2213))

the output is;-
socket bind is complete



Can you please tell me where i am making the error?
(The computers are connected as the ping operation is executing properly in
command prompt)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2013-04-06 Thread Alan Gauld

On 05/04/13 11:47, Mousumi Basu wrote:



s=socket.socket(socket.AF_INIT,socket.SCK_DGRM))


There is a double )) at the end of the line.
That should give a different error so I assume this is
not cut 'n paste code, but just in case I thought
I'd point it out...


s.bind(('172.18.2.11',8032))


Because I can't see anything wrong here.


But error is occurring showing "bind not done"


Can you send the full error text via cut 'n paste please?

--
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2013-04-05 Thread Chris Fuller

Have you checked out the socket HOWTO?
http://docs.python.org/2/howto/sockets.html

Cheers

On Friday, April 05, 2013, Mousumi Basu wrote:
> I want to perform bind function for  socket programming between two
> computers of ip addresses- 172.18.2.11 and 172.18.2.95 using the following
> command(on the computer having IP address 172.18.2.95):
> 
> s=socket.socket(socket.AF_INIT,socket.SCK_DGRM))
> s.bind(('172.18.2.11',8032))
> 
> But error is occurring showing "bind not done"
> 
> The ping operation of command prompt is showing that the computers are
> connected.
> 
> Please help me.

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2013-04-05 Thread Steven D'Aprano

On 05/04/13 21:47, Mousumi Basu wrote:

I want to perform bind function for  socket programming between two
computers of ip addresses- 172.18.2.11 and 172.18.2.95 using the following
command(on the computer having IP address 172.18.2.95):

s=socket.socket(socket.AF_INIT,socket.SCK_DGRM))



I get THREE errors with that line:

* SyntaxError due to an extra closing parenthesis;

* after fixing that problem, I get an AttributeError:
  'module' object has no attribute 'AF_INIT'

* after guessing what you mean instead of AF_INIT, I get another AttributeError:
  'module' object has no attribute 'SCK_DGRM'


Could you show us the code you are actually using, and save us from having to 
guess?



s.bind(('172.18.2.11',8032))

But error is occurring showing "bind not done"


Please copy and paste the complete traceback, starting with the line:


Traceback (most recent call last)


and going all the way to the end of the error message.





--
Steven
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Socket Programming

2013-04-05 Thread Mousumi Basu
I want to perform bind function for  socket programming between two
computers of ip addresses- 172.18.2.11 and 172.18.2.95 using the following
command(on the computer having IP address 172.18.2.95):

s=socket.socket(socket.AF_INIT,socket.SCK_DGRM))
s.bind(('172.18.2.11',8032))

But error is occurring showing "bind not done"

The ping operation of command prompt is showing that the computers are
connected.

Please help me.
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2012-01-29 Thread Navneet

On 1/27/2012 10:13 PM, Steven D'Aprano wrote:

Navneet wrote:

One more thing I want to add here is, I am trying to create the GUI 
based chat server.(Attached the programs.)



Please do not send large chunks of code like this, unless asked. 
Instead, you should try to produce a minimal example that demonstrates 
the problem. It should be:


* short (avoid code which has nothing to do with the problem)

* self-contained (other people must be able to run it)

* correct (it must actually fail in the way you say it fails)

See here for more: http://sscce.org/


In cutting your code down to a minimal example, 9 times out of 10 you 
will solve your problem yourself, and learn something in the process.




bash-3.1$ python Client1.py
Enter the server address:...9009
Traceback (most recent call last):
  File "Client1.py", line 53, in 
c = ClientChat(serverport)
  File "Client1.py", line 24, in __init__
gui.callGui()
  File "a:\FedEx\Exp\ClientGui.py", line 37, in callGui
sendbutton =Button(f2, width = 5, height = 2, text = "Send", 
command = C.ClientChat.senddata())
TypeError: unbound method senddata() must be called with ClientChat 
instance as first argument (got nothing instead)



This one is easy. You need to initialize a ClientChat instance first. 
This may be as simple as:


command = C.ClientChat().senddata

although I'm not sure if ClientChat requires any arguments.

Note that you call the ClientChat class, to create an instance, but 
you DON'T call the senddata method, since you want to pass the method 
itself as a callback function. The button will call it for you, when 
needed.





Thanks for the clarification and telling me about SSCCE :)

But just a simple thing,,, Can I call a method of another module while 
creating a GUI.


For example
 C = Tk()
.(Some more lines)
self.sendbutton =Button(self.f2, width = 5, height = 2, text = "Send", 
command = .)

self.sendbutton.pack(side = LEFT, padx = 10, pady = 10)
.(Some more lines)
C.mainloop()


Because I am getting stuck in a loop. The client is keep on connecting 
to server without creating a GUI.












___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2012-01-27 Thread Steven D'Aprano

Navneet wrote:

One more thing I want to add here is, I am trying to create the GUI 
based chat server.(Attached the programs.)



Please do not send large chunks of code like this, unless asked. Instead, you 
should try to produce a minimal example that demonstrates the problem. It 
should be:


* short (avoid code which has nothing to do with the problem)

* self-contained (other people must be able to run it)

* correct (it must actually fail in the way you say it fails)

See here for more: http://sscce.org/


In cutting your code down to a minimal example, 9 times out of 10 you will 
solve your problem yourself, and learn something in the process.




bash-3.1$ python Client1.py
Enter the server address:...9009
Traceback (most recent call last):
  File "Client1.py", line 53, in 
c = ClientChat(serverport)
  File "Client1.py", line 24, in __init__
gui.callGui()
  File "a:\FedEx\Exp\ClientGui.py", line 37, in callGui
sendbutton =Button(f2, width = 5, height = 2, text = "Send", command 
= C.ClientChat.senddata())
TypeError: unbound method senddata() must be called with ClientChat 
instance as first argument (got nothing instead)



This one is easy. You need to initialize a ClientChat instance first. This may 
be as simple as:


command = C.ClientChat().senddata

although I'm not sure if ClientChat requires any arguments.

Note that you call the ClientChat class, to create an instance, but you DON'T 
call the senddata method, since you want to pass the method itself as a 
callback function. The button will call it for you, when needed.




--
Steven

___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2012-01-27 Thread Navneet

On 1/26/2012 4:22 PM, Navneet wrote:

Hi,

I am trying to create a chat program.(Programs are attached)
Please find the code below, where I am having the problem.

def run( self ):
while 1:
print "Hello There "
print self.descriptors
# Await an event on a readable socket descriptor
(sread, swrite, sexc) = select.select( self.descriptors, 
[], [] )

print sexc
# Iterate through the tagged read descriptors
print sread
print "Hello There 1 "
for sock in sread:


For this I am getting the output as below:
bash-3.1$ python Server.py
Enter the Port:...9009
ChatServer started on port 9009
Hello There 
[]


But it is not printing the value of sread or "Hello There 1 !"






Hello All,

One more thing I want to add here is, I am trying to create the GUI 
based chat server.(Attached the programs.)

The concept is something like this:
I will start a server on a particular port and different Clients can 
interact with each other using that (more like a chat room )
Now I want to add the GUI part in a different module and want to call 
that module from client program.

But while executing the client1.py I am getting below error :



bash-3.1$ python Client1.py
Enter the server address:...9009
Traceback (most recent call last):
  File "Client1.py", line 53, in 
c = ClientChat(serverport)
  File "Client1.py", line 24, in __init__
gui.callGui()
  File "a:\FedEx\Exp\ClientGui.py", line 37, in callGui
sendbutton =Button(f2, width = 5, height = 2, text = "Send", 
command = C.ClientChat.senddata())
TypeError: unbound method senddata() must be called with ClientChat 
instance as first argument (got nothing instead)













from Tkinter import *
import tkMessageBox
import socket
import logging
import sys, time
import pickle
import thread
import ClientGui as gui



class ClientChat():
def __init__(self,serverport):
self.counter = 0

self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

self.host = 'localhost' # server address
self.port = serverport # server port 

# connect to server
self.s.connect((self.host, self.port))

gui.callGui()


def senddata(self):
self.k = gui.callGui.sendmsg.get(1.0, END)
gui.callGui.sendmsg.delete(1.0, END)
self.s.send(self.k)

if self.counter == 0:
thread.start_new_thread(self.recvdata,())
##if self.k == '':
##thread.exit()
##self.s.close()
##self.chatclient.destroy()

def exitchat():
self.s.close()
self.chatclient.destroy()

def recvdata(self):
while 1:
self.v = self.s.recv(1024) 
gui.callGui.recvmsg.insert(END,self.v)



if __name__ == '__main__':

serverport = int(raw_input("Enter the server address:..."))
c = ClientChat(serverport)


from Tkinter import *
import tkMessageBox
import socket
import logging
import sys, time
import pickle
import thread
import Client1 as C


def callGui():
chatclient = Tk()

chatclient["padx"] = 200
chatclient["pady"] = 200

chatclient.title("Client")

f1 = Frame(chatclient,width = 100, height = 10)
f1.pack(side = TOP)

f2 = Frame(chatclient,width = 100, height = 10)
f2.pack(side = BOTTOM)


recvmsg = Text(f1, width = 100, height = 10)
sb = Scrollbar(f1)
sb.pack(side = RIGHT, fill = Y)

sb.config(command = recvmsg.yview)
recvmsg.config(yscrollcommand=sb.set)
recvmsg.pack(padx = 10, pady =10)

sendmsg = Text(f2, width = 100, height = 5)
sendmsg.pack(padx = 10, pady =20)

sendbutton =Button(f2, width = 5, height = 2, text = "Send", command = 
C.ClientChat.senddata())
sendbutton.pack(side = LEFT, padx = 10, pady = 10)

sendbutton =Button(f2, width = 5, height = 2, text = "Exit", command = 
C.ClientChat.exitchat())
sendbutton.pack(side = RIGHT, padx = 10, pady = 10)

chatclient.mainloop()
import socket
import select


class ChatServer:
def __init__( self, port ):
self.port = port;
self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
self.srvsock.bind( ("localhost", port) )
self.srvsock.listen( 5 )
self.descriptors = [self.srvsock]
print 'ChatServer started on port %s' % port
def run( self ):
while 1:
# Await an event on a readable socket descriptor
sread, swrite, sexc = select.select( self.descriptors, [], [],5 )
# Iterate through the tagged read descriptors
for sock in sread:
# Received a connect to the server (listening) socket
if sock == self.srvsock:
self.accept_new_connection()
else:
# Received

[Tutor] Socket Programming

2012-01-26 Thread Navneet

Hi,

I am trying to create a chat program.(Programs are attached)
Please find the code below, where I am having the problem.

def run( self ):
while 1:
print "Hello There "
print self.descriptors
# Await an event on a readable socket descriptor
(sread, swrite, sexc) = select.select( self.descriptors, 
[], [] )

print sexc
# Iterate through the tagged read descriptors
print sread
print "Hello There 1 "
for sock in sread:


For this I am getting the output as below:
bash-3.1$ python Server.py
Enter the Port:...9009
ChatServer started on port 9009
Hello There 
[]


But it is not printing the value of sread or "Hello There 1 !"





import socket
import select


class ChatServer:
def __init__( self, port ):
self.port = port;
self.srvsock = socket.socket( socket.AF_INET, socket.SOCK_STREAM )
self.srvsock.setsockopt( socket.SOL_SOCKET, socket.SO_REUSEADDR, 1 )
self.srvsock.bind( ("", port) )
self.srvsock.listen( 5 )
self.descriptors = [self.srvsock]
print 'ChatServer started on port %s' % port
def run( self ):
while 1:
print "Hello There "
print self.descriptors
# Await an event on a readable socket descriptor
(sread, swrite, sexc) = select.select( self.descriptors, [], [] )
print sexc 
# Iterate through the tagged read descriptors
print sread
print "Hello There 1 "
for sock in sread:
# Received a connect to the server (listening) socket
if sock == self.srvsock:
self.accept_new_connection()
else:
# Received something on a client socket
str = sock.recv(100)
# Check to see if the peer socket closed
if str == '':
host,port = sock.getpeername()
str = 'Client left %s:%s\r\n' % (host, port)
self.broadcast_string( str, sock )
sock.close()
self.descriptors.remove(sock)
else:
host,port = sock.getpeername()
newstr = '[%s:%s] %s' % (host, port, str)
self.broadcast_string( newstr, self.srvsock )

def accept_new_connection( self ):
newsock, (remhost, remport) = self.srvsock.accept()
self.descriptors.append( newsock )
newsock.send("You're connected to the Python chatserver\r\n")
str = 'Client joined %s:%s\r\n' % (remhost, remport)
self.broadcast_string( str, newsock )

def broadcast_string( self, str, omit_sock ):
for sock in self.descriptors:
if sock != self.srvsock and sock != omit_sock:
sock.send(str)
print str,

if __name__ == '__main__':

portno = int(raw_input("Enter the Port:..."))
s = ChatServer(portno)
s.run()

from Tkinter import *
import tkMessageBox
import socket
import logging
import sys, time
import pickle
import thread



class ClientChat():
def __init__(self,serverport):
self.counter = 0

self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

self.host = 'localhost' # server address
self.port = serverport # server port 

# connect to server
self.s.connect((self.host, self.port))

self.chatclient = Tk()

self.chatclient["padx"] = 200
self.chatclient["pady"] = 200

self.chatclient.title("Client")

self.f1 = Frame(self.chatclient,width = 100, height = 10)
self.f1.pack(side = TOP)

self.f2 = Frame(self.chatclient,width = 100, height = 10)
self.f2.pack(side = BOTTOM)


self.recvmsg = Text(self.f1, width = 100, height = 10)
self.sb = Scrollbar(self.f1)
self.sb.pack(side = RIGHT, fill = Y)

self.sb.config(command = self.recvmsg.yview)
self.recvmsg.config(yscrollcommand=self.sb.set)
self.recvmsg.pack(padx = 10, pady =10)

self.sendmsg = Text(self.f2, width = 100, height = 5)
self.sendmsg.pack(padx = 10, pady =20)

self.sendbutton =Button(self.f2, width = 5, height = 2, text = "Send", 
command = self.senddata)
self.sendbutton.pack(side = LEFT, padx = 10, pady = 10)

self.sendbutton =Button(self.f2, width = 5, height = 2, text = "Exit", 
command = self.exitchat)
self.sendbutton.pack(side = RIGHT, padx = 10, pady = 10)

self.chatclient.mainloop()


def senddata(self):
self.k = self.sendmsg.get(1.0, END)
self.sendmsg.delete(1.0, END)
self.s.send(self.k)

if self.counter == 0:
thread.start_new_thread(self.recvdata,())
##if self.k == '':
##   

Re: [Tutor] Socket Programming issue

2011-06-21 Thread Mark Tolonen


"aditya"  wrote in message 
news:BANLkTikS+gzm=89thczpbwksd+wufec...@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= 2
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 = 2
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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor




___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming issue

2011-06-21 Thread aditya
On Tue, Jun 21, 2011 at 12:45 PM, Alan Gauld wrote:

>
> "aditya"  wrote
>
>
>  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 ,
>>
>
> Use the return values from functions.
>
>
>  CLIENT.PY
>>
>> import socket
>> import os
>> import sys
>> os.system("C:\Python26\ping.**vbs")
>> host = "localhost"
>> port= 2
>> 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()
>>
>
> You could print the data here to check that you got something
> meaningful from the file
>
>
>  if data: s.send(data)
>>
>
> The socket documentation specifically says:
> --**---
> socket.send(string[, flags])
>  Send data to the socket. The socket must be connected to a remote socket.
> The optional flags argument has the same meaning as for recv() above.
> Returns the number of bytes sent. Applications are responsible for checking
> that all data has been sent; if only some of the data was transmitted, the
> application needs to attempt delivery of the remaining data.
>
> >Thanks Alan i will put in some conditional checks to check and reattempt
to send the data if it has not been sent or not. besides i am sending you
the client,server, and the output file to just go though it once . You can
run the script for yourself and see what happens.It actually prints only one
line of the ouput file and some times all the lines at the server end.

Besides can you help me with the checks which you insist me to put in
socket.send() ?







> --**
> Notice that it tells you how many bytes have been sent, and it
> is your responsibility to check and retry if necessary.
> I see no check nor any attempt to retry
>
> That should tell you if the data is leaving the client at least.
>
>
>
>  SERVER.PY
>> import socket
>>
>> host = ""
>> port = 2
>> size = 1024
>> backlog=5
>> s=socket.socket(socket.AF_**INET, socket.SOCK_STREAM)
>>
>> s.bind((host,port))
>> s.listen(backlog)
>>
>> while 1:
>> client, addr =s.accept()
>> data=client.recv(size)
>> if data:
>> print data
>> else:
>> print  "client exit"
>>
>
> This implies you should always get something printed, do you?
> When you say it sometimes prints the lines and sometimes
> not, does it print the exit message instead?
>
>  s.close()
>>
>
> I'm not clear where this line sits since the mail has lost
> its indentation. Is it inside the else or at the while level?
>
> Just some things to c0onsider,
>
> HTH
>
>
> --
> Alan Gauld
> Author of the Learn to Program web site
> http://www.alan-g.me.uk/
>
>
>
> __**_
> Tutor maillist  -  Tutor@python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/**mailman/listinfo/tutor
>



-- 
Regards
Aditya
reply from 192.168.100.1
reply from 192.168.100.7


client.py
Description: Binary data


server.py
Description: Binary data
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming issue

2011-06-21 Thread Alan Gauld


"aditya"  wrote

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 ,


Use the return values from functions.


CLIENT.PY

import socket
import os
import sys
os.system("C:\Python26\ping.vbs")
host = "localhost"
port= 2
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()


You could print the data here to check that you got something
meaningful from the file


if data: s.send(data)


The socket documentation specifically says:
-
socket.send(string[, flags])
 Send data to the socket. The socket must be connected to a remote 
socket. The optional flags argument has the same meaning as for recv() 
above. Returns the number of bytes sent. Applications are responsible 
for checking that all data has been sent; if only some of the data was 
transmitted, the application needs to attempt delivery of the 
remaining data.


--
Notice that it tells you how many bytes have been sent, and it
is your responsibility to check and retry if necessary.
I see no check nor any attempt to retry

That should tell you if the data is leaving the client at least.



SERVER.PY
import socket

host = ""
port = 2
size = 1024
backlog=5
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((host,port))
s.listen(backlog)

while 1:
client, addr =s.accept()
data=client.recv(size)
if data:
print data
else:
print  "client exit"


This implies you should always get something printed, do you?
When you say it sometimes prints the lines and sometimes
not, does it print the exit message instead?


s.close()


I'm not clear where this line sits since the mail has lost
its indentation. Is it inside the else or at the while level?

Just some things to c0onsider,

HTH


--
Alan Gauld
Author of the Learn to Program web site
http://www.alan-g.me.uk/



___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Socket Programming issue

2011-06-20 Thread aditya
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= 2
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



f.close()

s.close()



SERVER.PY


import socket

host = ""
port = 2
size = 1024
backlog=5
s=socket.socket(socket.AF_INET, socket.SOCK_STREAM)

s.bind((host,port))
s.listen(backlog)

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@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2005-08-01 Thread Adam Bark
Hi Joe you can use 1 and just send 'Hi!' as long as something else
isn't likely to be sent at the same time. It will just read what is in
that socket when you call it so if something else that you don't want
is there just behind what you're after then you could end up with that
as well.On 8/1/05, Joseph Quigley <[EMAIL PROTECTED]> wrote:
Hi,Ok. But what if I wanted to send a 1 mb message? Could I leave.recv(1) in and send 'Hi!' as well or would I have to change it?Thanks,JQAdam Bark wrote:> You have to put in how many bytes of data you want to recieve in
> clientsocket.recv() eg. clientsocket.recv(1024) for 1kB.
___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2005-08-01 Thread Joseph Quigley
Hi,

Ok. But what if I wanted to send a 1 mb message? Could I leave 
.recv(1) in and send 'Hi!' as well or would I have to change it?
Thanks,
JQ

Adam Bark wrote:

> You have to put in how many bytes of data you want to recieve in 
> clientsocket.recv() eg. clientsocket.recv(1024) for 1kB.


___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2005-08-01 Thread Joseph Quigley
Hi Johan,

Johan Geldenhuys wrote:

> I have more advanced examples if you want them, but start here and 
> look at the socket lib and you can start using select if you want to 
> wait for stuff from the server or client.
>
> Johan



I'd love to see your examples. Could you send them directly to me as an 
attachment? I've got a question... how can I add more clients? I'm 
trying to make a type of bot.
Thanks for your help. Your example worked perfectly!

Joe

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2005-08-01 Thread Adam Bark
You have to put in how many bytes of data you want to recieve in clientsocket.recv() eg. clientsocket.recv(1024) for 1kB.On 7/31/05, Joseph Quigley <
[EMAIL PROTECTED]> wrote:Hi Dan,
Danny Yoo wrote:>##>clientsocket.recv()>##>>should work better.>>Ok well I tried your examples but they didn't work. I still get:TypeError: recv() takes at least 1 argument (0 given)
How can I fix this?___Tutor maillist  -  Tutor@python.orghttp://mail.python.org/mailman/listinfo/tutor

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2005-08-01 Thread Joseph Quigley
Hi Dan,

Danny Yoo wrote:

>##
>clientsocket.recv()
>##
>
>should work better.
>  
>
Ok well I tried your examples but they didn't work. I still get:
TypeError: recv() takes at least 1 argument (0 given)

How can I fix this?

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2005-07-31 Thread Kent Johnson
Joseph Quigley wrote:
> I have no idea how to get the server to receive and print a message or 
> for the client to send the message. Does anyone know of some good python 
> networking tutorials designed for newbies? (Not ones on 
> www.awaretek.com. They seem to expect you to know some things that I 
> don't even know about.)

Python in a Nutshell shows several ways to write a simple echo server in Python 
using bare TCP sockets and higher-level libraries. The examples are available 
online (chapter 19). 
http://www.oreilly.com/catalog/pythonian/

There is a book on Python Network Programming, it's on sale at Bookpool: 
http://www.bookpool.com/sm/1590593715

Kent

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Socket Programming

2005-07-30 Thread Danny Yoo


> I have no idea how to get the server to receive and print a message or
> for the client to send the message. Does anyone know of some good python
> networking tutorials designed for newbies? (Not ones on
> www.awaretek.com. They seem to expect you to know some things that I
> don't even know about.)

Hi Joe,

Before we start: it might be easier to start off with a high-level
networking protocol, like XMLRPC.  There are examples of making xmlrpc
clients and servers in the Library documentation:

http://www.python.org/doc/lib/xmlrpc-client-example.html
http://www.python.org/doc/lib/simple-xmlrpc-servers.html


Sockets tend to be pretty low-level at times, with its own set of issues.
Have you seen AMK's Socket HOWTO tutorial already?

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

... wait, ok, I see, your example code below appears to come from the
Socket HOWTO tutorial after all!  Ok, so you're trying those examples now?
Let's take a closer look.


> #Client
> import socket
> #create an INET, STREAMing socket
> s = socket.socket(
> socket.AF_INET, socket.SOCK_STREAM)
> s.connect(("localhost", 2000))
> s.send('hello!'[totalsent:])


The last statement looks a little weird, since 'totalsent' isn't defined.
Are you just trying to send a message to the server?  If so, then:

##
s.send("hello!\n")
##

should do something, although there's no guarantee that all seven bytes
will go across the network.  So something like:

##
bytesReallySent = 0
while bytesReallySent != len('hello!\n'):
bytesReallySent += s.send("hello!\n"[bytesReallySent:])
##

might work better: it will continue calling s.send() until all the bytes
in the message have been sent over.  Of course, this is really paranoid:
the socket probably will be able to pass all seven bytes at once, but for
longer strings, a socket's send() method doesn't guarantee how much it can
send at once.


As you can see, this is a really low-level detail sort of thing: we can
avoid some of these issues by using a higher-level file interface:

##
import socket
s = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 2000))
f = s.makefile()
##

At this point, f is a file-like object, and we should be able to use a
file's write() method:

##
f.write('hello!\n')
##

and the high-level interface should guarantee that this will work in one
shot.


However, even then, we may need to worry about buffering and blocking
issues, so we may need to use a flush() to guarantee that things are
passed across the network.

##
f.flush()
##



Let's look at the server side code:

> #Server
> import socket
> #create an INET, STREAMing socket
> serversocket = socket.socket(
> socket.AF_INET, socket.SOCK_STREAM)
> #bind the socket to a public host,
> # and a well-known port
> serversocket.bind((socket.gethostname(), 2000))
> #become a server socket
> serversocket.listen(5)
>
> while 1:
> #accept connections from outside
> clientsocket, address = serversocket.accept()
> serversocket.recv()
  

I believe the last statement is off; you probably want to use the
'clientsocket' here, not 'serversocket'.

The idea is that there's a central serversocket that listens to a
particular port, and that's the socket that's being bound in:

serversocket.bind((socket.gethostname(), 2000))

And every time a client comes, it talks to the serversocket.  The
serversocket accepts the connection, but delegates communication off to
the clientsocket instead, the one that gets returned from
serversocket.accept():

clientsocket, address = serversocket.accept()

So:

##
clientsocket.recv()
##

should work better.

The Socket Programming HOWTO talks about the difference between the
"server socket" and a "client socket" in a little more detail, near the
end of Section 2.


If you have more questions, please feel free to ask!

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor


[Tutor] Socket Programming

2005-07-30 Thread Joseph Quigley
I've got a little problem with my socket program(s):

#Client
import socket
#create an INET, STREAMing socket
s = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
s.connect(("localhost", 2000))
s.send('hello!'[totalsent:])

#Server
import socket
#create an INET, STREAMing socket
serversocket = socket.socket(
socket.AF_INET, socket.SOCK_STREAM)
#bind the socket to a public host,
# and a well-known port
serversocket.bind((socket.gethostname(), 2000))
#become a server socket
serversocket.listen(5)

while 1:
#accept connections from outside
clientsocket, address = serversocket.accept()
serversocket.recv()

I have no idea how to get the server to receive and print a message or 
for the client to send the message. Does anyone know of some good python 
networking tutorials designed for newbies? (Not ones on 
www.awaretek.com. They seem to expect you to know some things that I 
don't even know about.)

Thanks,
Joe Q.

___
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor