Python socket client works only for one iteration - python

I am trying to implement sockets with python.the following code works well without the while loop..but with the while loop, for the second iteration , it gets stuck in s.sendall().could you please suggest how to fix this ?
def main():
host = socket.gethostname()
port = 11111
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
print "Connected to Server!"
while True:
print "Enter your Command:"
command = raw_input()
if(command):
try:
sock.sendall(repr(command))
except socket.error:
print "Socket Error Occured"
data = sock.recv(1024)
if data:
print('Received', repr(data))
else:
print "No Data"
else:
os.system("clear")
sock.close()
if __name__ == "__main__":
main()

Hello
I don't have all the information I need to make this post as
informed as I'd like, however I can make some educate
d guesses and try my best to explain what I think is going wrong. Are you ready?? Lets get into it.
So,
You start off by making a tcp socket and then connecting to a server hosted locally on port 11111
host = socket.gethostname()
port = 11111
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
next you enter a loop
while True:
#input command, request eval
Here your goal is to take user input and send it to a server to eval. You do that with
#get user input
print "Enter your Command:"
command = raw_input()
#send command to server for eval
sock.sendall(repr(command))
#receive then print eval
data = sock.recv(1024)
print('Received', repr(data))
this works and sends commands as you'd expect, although sending repr(command) might not be what you want to send
command = "1+1"
eval(command)
//2
eval(repr(command))
//'1+1'
Now
Here is where I have to make some assumptions
Early on you connected to a server
sock.connect((host, port))
I'm assuming that the server accepts your connection evals your command and sends the answer back. Something like
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind('localhost',11111)
sock.listen()
while True:
conn, addr = sock.accept()
command = conn.recv(1024)
sock.sendall(eval(command))
If this is the case then your connection might fail because your eval server runs eval once and then accepts a new connection.
That means that your client can no longer send to, or recieve data from, the server
I hope this helps.

Related

Python socket wont stop spam

I am sending a message to client but the client wont stop spamming the message
server.py
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
hostname = socket.gethostname()
hostip = socket.gethostbyname(hostname)
port = 462
server.bind((hostname, port))
server.listen(1)
print((hostip, port))
client, address = server.accept()
print("New connection!: ", address)
while True:
data = input("Do something:")
if data == "help":
print("test: 'test'")
input("Click ENTER to continue")
elif data == "test":
client.send("test".encode('ascii'))
else:
continue
client.py
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = socket.gethostname()
port = 462
s.connect(('', port))
data = s.recv(1024)
while True:
if data.decode('ascii') == "test":
print(data.decode('ascii'))
else:
continue
You didn't post the full example, but I think I can see where the problem is.
Where/when do you actually read/receive the data from the socket in the client.py?
Because it looks like you received the data, saved it in the in the "data" variable and then you keep looping forever decoding this data, not reading from socket.
So I think you need to move your while loop in client.py outside, so that the read/receive from socket method is inside your loop, not outside as it appears it is now.
Edit: Yup, from the full code that you posted I can see that indeed, this should fix your problem.

TCP Python Socket Server Response

I'm writing a TCP python script that will act as a server and retrieve a temperature reading from a machine (the client). I need to pass a command to the client from the server and listen for an output of the response. I successfully reach the cmd definition line, but when s.accept() is called I'm left hanging with no response from the client.
Server.py
import socket
port = 7777
ip = raw_input('192.168.62.233')
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((ip, port))
s.listen(1)
print "waiting on port: ", port
while True:
cmd = raw_input('KRDG? A[term]')#command send to client
conn, addr = s.accept()
s.send(cmd)
print "It sent"
data = conn.recv(4096)
print "Received:", data, " from address ", addr
Edit:
I believe you're correct, I should consider my code the client and temperature readout at the server. I do now get left hanging after "here 2" when I go to s.recv().
Client.py
import socket
ip = '192.168.62.233'
port = 7777 # The same port as used by the server
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect((ip, port))
print 'here'
s.send('KRDG? A[term]')
print 'here 2'
data = s.recv(4096)
print 'here 3'
s.close()
print 'Received', repr(data)
Usually, a TCP server accept will block while it waits for a client to connect. Have you checked to make sure that you client can and is connecting? You could use a tool like tcpdump or similar to watch the network activity and make sure the client is connecting.

Python server programming

I am trying a little client server project to get me into network programming but I seem to have got stuck at the first hurdle. I cant seem to get past getting the first line of data only even if its a new connection.
#!/usr/bin/python
import socket
s = socket.socket()
host = '192.168.0.233' # Test Server
port = 7777
s.bind((host, port))
s.listen(5)
while True:
c, addr = s.accept()
print 'Got connection from', addr
data = c.recv(2048)
print(data)
If I telnet to the host running the server, the connection opens fine and I see on the server Got connection from addr, but I also only see the first line of data when I sent 4 lines of data,
I thought because its in a loop it should now always be looking for data?
I know im doing something wrong but unsure what.
Im using Python 2.6.6
recv needs to be in a loop too, at the moment your code is receiving some data and then waiting for a new connection.
https://docs.python.org/2/library/socket.html#example has an example of socket.recv in a loop.
Try this:
#!/usr/bin/python
import socket
import threading
def listenForClients(sock):
while True:
client, address = sock.accept()
client.settimeout(5)
threading.Thread( target = listenToClient, args = (client,address) ).start()
def listenToClient(client, address):
size = 2048
while True:
try:
data = client.recv(size)
if data:
response = "Got connection"
client.send(response)
else:
raise error('Client disconnected')
except:
client.close()
return False
def main(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((host, port))
sock.listen(5)
listenForClients(sock)
if __name__ == "__main__":
main('192.168.0.233',7777)
Here I use a thread for each client. The problem that you have with having Socket.accept() in the loop is that it blocks meaning that concurrent access won't work and you'll only be able to talk to one client at a time.
Try running it in the background and sending it messages with:
#!/usr/bin/python
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('192.168.0.233',7777))
vwhile True:
data = raw_input("enter a message: ")
sock.send(data)
print sock.recv(2048)

Sending string via socket (python)

I have two scripts, Server.py and Client.py.
I have two objectives in mind:
To be able to send data again and again to server from client.
To be able to send data from Server to client.
here is my Server.py :
import socket
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "192.168.1.3"
port = 8000
print (host)
print (port)
serversocket.bind((host, port))
serversocket.listen(5)
print ('server started and listening')
while 1:
(clientsocket, address) = serversocket.accept()
print ("connection found!")
data = clientsocket.recv(1024).decode()
print (data)
r='REceieve'
clientsocket.send(r.encode())
and here is my client :
#! /usr/bin/python3
import socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host ="192.168.1.3"
port =8000
s.connect((host,port))
def ts(str):
s.send('e'.encode())
data = ''
data = s.recv(1024).decode()
print (data)
while 2:
r = input('enter')
ts(s)
s.close ()
The function works for the first time ('e' goes to the server and I get return message back), but how do I make it happen over and over again (something like a chat application) ?
The problem starts after the first time. The messages don't go after the first time.
what am I doing wrong?
I am new with python, so please be a little elaborate, and if you can, please give the source code of the whole thing.
import socket
from threading import *
serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = "192.168.1.3"
port = 8000
print (host)
print (port)
serversocket.bind((host, port))
class client(Thread):
def __init__(self, socket, address):
Thread.__init__(self)
self.sock = socket
self.addr = address
self.start()
def run(self):
while 1:
print('Client sent:', self.sock.recv(1024).decode())
self.sock.send(b'Oi you sent something to me')
serversocket.listen(5)
print ('server started and listening')
while 1:
clientsocket, address = serversocket.accept()
client(clientsocket, address)
This is a very VERY simple design for how you could solve it.
First of all, you need to either accept the client (server side) before going into your while 1 loop because in every loop you accept a new client, or you do as i describe, you toss the client into a separate thread which you handle on his own from now on.
client.py
import socket
s = socket.socket()
s.connect(('127.0.0.1',12345))
while True:
str = raw_input("S: ")
s.send(str.encode());
if(str == "Bye" or str == "bye"):
break
print "N:",s.recv(1024).decode()
s.close()
server.py
import socket
s = socket.socket()
port = 12345
s.bind(('', port))
s.listen(5)
c, addr = s.accept()
print "Socket Up and running with a connection from",addr
while True:
rcvdData = c.recv(1024).decode()
print "S:",rcvdData
sendData = raw_input("N: ")
c.send(sendData.encode())
if(sendData == "Bye" or sendData == "bye"):
break
c.close()
This should be the code for a small prototype for the chatting app you wanted.
Run both of them in separate terminals but then just check for the ports.
This piece of code is incorrect.
while 1:
(clientsocket, address) = serversocket.accept()
print ("connection found!")
data = clientsocket.recv(1024).decode()
print (data)
r='REceieve'
clientsocket.send(r.encode())
The call on accept() on the serversocket blocks until there's a client connection. When you first connect to the server from the client, it accepts the connection and receives data. However, when it enters the loop again, it is waiting for another connection and thus blocks as there are no other clients that are trying to connect.
That's the reason the recv works correct only the first time. What you should do is find out how you can handle the communication with a client that has been accepted - maybe by creating a new Thread to handle communication with that client and continue accepting new clients in the loop, handling them in the same way.
Tip: If you want to work on creating your own chat application, you should look at a networking engine like Twisted. It will help you understand the whole concept better too.

Python - Server and client problems

I'm trying to create a basic server and client script. The idea is that the client can connect to the server and execute commands. Kinda like SSH but very simple. Heres my server code:
import sys, os, socket
host = ''
port = 50103
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((host, port))
print("Server started on port: ", port)
s.listen(1)
while (1):
conn, addr = s.accept()
print 'New connection from ', addr
try:
while True:
rc = conn.recv(2)
pipe = os.popen(rc)
rl = pipe.readlines()
fl = conn.makefile('w', 0)
fl.writelines(rl[:-1])
fl.close()
except IOError:
conn.close()
And here is my client:
import sys, socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
host = 'localhost'
port = input('Port: ')
s.connect((host, port))
while (1):
cmd = raw_input('$ ')
s.send(cmd)
file = s.makefile('r', 0)
sys.stdout.writelines(file.readlines())
file.close()
Here is my problem. I start the server and then run the client on the same machine. I enter the port and connect. Then I get the raw_input which is the '$'. If I type a command like 'ls' it just hangs on the client side. I have to exit the server for the client to receive the output of ls. By the way I am running Ubuntu Linux. Not sure if that matters.
When you makefile() on the socket and then use readlines() on it, it will continue until you reach an end of file, which in the socket case is that it closed from the other end.
Using makefile() in this case makes no sense to me, especially since you create it and close it after each command. Just use send() and recv() on both ends.
You probably also want to have some sort of actual "protocol" so the server tells the client "HERE COMES A RESPONSE" and "THIS IS THE END OF THE RESPONSE" so that the client knows. Otherwise it gets hard to know when to stop waiting for more response. :)
Update with an example that works:
server.py:
import sys, os, socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 50500))
print("Server started")
s.listen(1)
while True:
print "Accepting"
conn, addr = s.accept()
print 'New connection from ', addr
while True:
try:
rc = conn.recv(1024)
print "Command", rc
if not rc.strip():
continue
if rc.strip() == 'END':
print "Close"
conn.send("**END**")
conn.close()
break
else:
conn.send("This is the result of command %s\n" % rc)
except Exception:
conn.close()
sys.exit()
client.py
import sys, os, socket
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 50500))
while True:
cmd = raw_input('$ ')
s.send(cmd)
result = s.recv(1024)
print result
if result == "**END**":
print "Ending"
break
Well for one thing you're only connecting on the client once and on the server you're closing the socket after every read.
You should take a look at this example.
http://ilab.cs.byu.edu/python/socket/echoserver.html
You're doing quite a few things incorrectly.

Categories