I am writing a simple TCP server in python, and am trying to input a timeout. My current code:
import socket
def connect():
HOST = '' # Symbolic name meaning the local host
PORT = 5007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
s.settimeout(5)
conn, addr = s.accept()
print 'Connected by', addr
return conn
conn = connect()
while 1:
data = conn.recv(1024)
if not data: break
print data
conn.close()
Issue is when I try to connect I get an error at data = conn.recv(1024)
error: [Errno 10035] A non-blocking socket operation could not be completed immediately
Code works without the timeout.
You can turn on blocking:
# ...
conn.setblocking(1)
return conn
# ...
Try to set the timeout on the socket and the blocking on the connection. Like this:
import socket
def connect():
HOST = '' # Symbolic name meaning the local host
PORT = 5007 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(5)
s.bind((HOST, PORT))
s.listen(1)
return s
s = connect()
while 1:
conn, addr = s.accept()
print 'Connected by', addr
conn.setblocking(1)
data = conn.recv(1024)
conn.close()
if not data: break
print data
s.close()
Ran into the same problem 30 minutes ago. Found a simple non-elegant work around...if you give the socket time to breathe by doing time.sleep(1), catching the 10035 error and doing a retry it works. I'm using 2.7.5...maybe this is a bug that got fixed. Not real sure.
Code sample...please understand this is very simplistic test code I use (only recv 1 byte at a time).So where 's' is the socket with a 10s timeout and 'numbytes' is number of bytes I need...
def getbytes(s,numbytes):
din = ''
socketerror10035count = 0
while True:
try:
r = s.recv(1).encode('hex')
din += r
if len(din)/2 == numbytes:
print 'returning',len(din)/2, 'bytes'
break
except socket.timeout as e:
din = 'socket timeout'
break
except socket.error as e:
if e[0] == 10035 and socketerror10035count < 5:
socketerror10035count = socketerror10035count +1
time.sleep(1)
else:
din = 'socket error'
break
except:
din = 'deaddead'
break
return din
For Python 3 and above, the above code which references e as a scriptable object will need to be changed to "e.errno". And, of course the print statements require parenthesis around the arguments.
Additionally, you may want to change the "except socket.error as e:" line to "except BlockingIOError as e:". However, the code works as is under Python 3.8.5 on Windows.
Related
My server.py
import socket
s = socket.socket()
port = 12345
while True:
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)
c.send(rcvdData.encode())
if(sendData == "Bye" or sendData == "bye"):
break
c.close()
My client.py
import socket
s = socket.socket()
s.connect(('127.0.0.1',12345))
while True:
str = input("S: ")
s.send(str.encode());
if(str == "Bye" or str == "bye"):
break
print ("N:",s.recv(1024).decode())
s.close()
I have read to try to add try/except but im not sure where should i add that. Sorry for my english.
I want to accept connections even if no one is waiting to be accepted.
Multiple client connections are handled by calling accept, once per client. Nominally, you could just add a while loop plus exception handling to manage the connections:
import socket
import struct
s = socket.socket()
port = 12345
while True:
s.bind(('', port))
s.listen(5)
while True:
c, addr = s.accept()
print ("Socket Up and running with a connection from",addr)
try:
while True:
rcvdData = c.recv(1024).decode()
print("S:",rcvdData)
c.send(rcvdData.encode())
if(sendData == "Bye" or sendData == "bye"):
break
except (OSError, socket.error) as e:
print("Error:", e)
# reset connection
c.setsockopt(socket.SOL_SOCKET, socket.SO_LINGER, struct.pack('ii', 1, 0))
c.close()
else:
c.shutdown(socket.SHUT_RDWR)
c.close()
This will only allow a single client connection at at time. If you want to handle multiple connections at once, there are a hundred ways to do it and that's beyond the scope of an SO answer.
I have a Moxa device that create Tcp-ip message from serial data and send them to me by LAN.
i need to listen to hes specific external-ip(172.16.0.77) with python server.
ill try to do this:
BUFFER_SIZE = 10000 # Normally 1024, but we want fast response
HOST = self.TCP_IP #(172.16.0.77)
PORT = self.TCP_PORT # Arbitrary non-privileged port
s = None
for res in socket.getaddrinfo(HOST, PORT, socket.AF_UNSPEC,
socket.SOCK_STREAM, 0, socket.AI_PASSIVE):
af, socktype, proto, canonname, sa = res
try:
s = socket.socket(af, socktype, proto)
except socket.error as msg:
print msg
s = None
continue
try:
s.bind(sa)
s.listen(1)
except socket.error as msg:
print msg
s.close()
s = None
continue
break
if s is None:
print 'could not open socket'
while s:
print s.getsockname()
conn, addr = s.accept()
print 'Connection address:', addr
data = conn.recv(BUFFER_SIZE)
if data:
self.emit(QtCore.SIGNAL("SamplesRecive"),data)
conn.close()
and i get : [Errno 10049] The requested address is not valid in its context could not open socket
I need to divide the serves to many Moxa devices so i cant use socket.INADDR_ANY
Any ideas?
socket.INADDR_ANY equal to socket.bind('0.0.0.0')
if bind to "0.0.0.0" can listen all interfaces(which avaible)
Example of Moxa TCP :
import socket,time
import thread
#Example client
class _client :
def __init__(self):
self.status = False
def run(self,clientsock,addr):
while 1 :
try:
data = clientsock.recv(BUFF)
if data :
#do something with data
time.sleep(.1)
if self.status == False:
clientsock.close()
break
clientsock.send(next_query)
except Exception,e :
print e
break
client = _client()
ADDR = ("0.0.0.0", 45500) #so you need port how to decode raw socket data ?
serversock = socket(AF_INET, SOCK_STREAM)
serversock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
serversock.setsockopt(IPPROTO_TCP, TCP_NODELAY, 1)#gain 50ms tcp delay
serversock.bind(ADDR)
serversock.listen(10)
While True :
clientsock, addr = serversock.accept()
if addr[0] == device_1_IP:
client.status = True
#start device1 thread(moxa is TCP client mode)
thread.start_new_thread(client.run, (clientsock, addr))
#if client.status = False you will be close connection.
But my offer is "use moxa with TCP server mode"
i am used 4x5450i 3x5250 over 120 device without any error.
How can I have a Python socket server tell me when there is a connection and then CONTINUE to give me back data that the client sends? When I do it, it connects and then just loops over, telling me it connected over and over again.
I,just want it to connect and then continually check (or grab) data sent to the server.
Also, how can I tell if the client disconnected?
address = ('', 7777)
server_socket = socket(AF_INET, SOCK_STREAM)
server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
try:
server_socket.bind(address)
except Exception, e:
print colored("Address already in use", 'red')
server_socket.listen(2)
print colored("Socket ready", 'blue')
while True:
client_socket, addr = server_socket.accept()
hostIP = addr[0]
port = addr[1]
try:
host = gethostbyaddr(hostIP)[0]
except:
host = hostIP
print colored("Got connection from: " + host, 'blue')
try:
recv_data = server_socket.recv(2048)
print("Got: " + recv_data)
except:
print "nothing"
recv_data = "" # this is because I test what it is later, but that's irrevlevant.
Thanks
You didn't do anything with client_socket; ie: the actual client connection. Furthermore, the server
cannot know how much the client wants to send and so it must CONTINUE (ie: in a
loop) to receive data. When the connection sends 'empty' data, the connection
is terminated and the server goes back to listening. If you want the server to
accept new connections and continue to receive data from existing connections
look up the threading module.
import socket
address = ('', 7777)
server_socket = socket.socket(AF_INET, SOCK_STREAM)
server_socket.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
try:
server_socket.bind(address)
except Exception, e:
print colored("Address already in use", 'red')
server_socket.listen(2)
print colored("Socket ready", 'blue')
while True:
client_socket, addr = server_socket.accept()
hostIP = addr[0]
port = addr[1]
try:
host = gethostbyaddr(hostIP)[0]
except:
host = hostIP
print colored("Got connection from: " + host, 'blue')
while True:
try:
recv_data = client_socket.recv(2048)
if not recv_data:
break
print("Got: " + recv_data)
except socket.error, e:
print "nothing"
recv_data = "" # this is because I test what it is later, but that's irrevlevant.
I have been looking at some code for a small chat program that I found online. It was originally written for 2.7, but it seems to work with 3.2. The only problem is that I cannot send strings, only numbers:
The chat.py file source code:
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print ('Connected by ' + str(addr))
i = True
while i is True:
data = conn.recv(1024)
print ("Received " + repr(data))
reply = str(input("Reply: "))
conn.send(reply)
conn.close()
And the client.py source file:
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.connect((HOST, PORT)) # client-side, connects to a host
while True:
message = str(input("Your Message: "))
s.send(message)
print ("Awaiting reply...")
reply = s.recv(1024) # 1024 is max data that can be received
print ("Received " + repr(reply))
s.close()
When I run these using two separate terminals, they work, but do not send strings.
Thank you
When you work with sockets, the message you're passing around should probably be in bytes, b'bytes'. In Python 2.x, a str is actually what a bytes is in Python 3.x
So your message should be something like:
message = b'Message I want to pass'
Check here http://docs.python.org/3.3/library/stdtypes.html for more information.
According to http://docs.python.org/3/library/functions.html#input input returns a str, which means you'll have to encode message into bytes as such:
message = message.encode()
Do verify that this is the right approach to convert str to bytes by checking the type of message.
Your socket code is correct, it was just failing due to an unrelated error due to raw_input vs input. You probably intended to read a string from the shell instead of reading a string and trying to evaluate it as Python code which is what input does.
Try this instead:
chat.py
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.bind((HOST, PORT))
s.listen(1)
conn, addr = s.accept()
print ('Connected by ' + str(addr))
i = True
while i is True:
data = conn.recv(1024)
print ("Received " + repr(data))
reply = str(raw_input("Reply: "))
conn.send(reply)
conn.close()
client.py
from socket import *
HOST = ''
PORT = 8000
s = socket(AF_INET, SOCK_STREAM)
s.connect((HOST, PORT)) # client-side, connects to a host
while True:
message = str(raw_input("Your Message: "))
s.send(message)
print ("Awaiting reply...")
reply = s.recv(1024) # 1024 is max data that can be received
print ("Received " + repr(reply))
s.close()
In the code shown below I am using the blocking call to receive 50 bytes of data from socket and echo it back.But what is happening is that the code stuck after receiving one byte.In the telnet running on another command prompt the connection still shows as connected. What might be missing from this ?
import socket
import sys
host = ''
port = 8888
s = socket.socket(socket.AF_INET,socket.SOCK_STREAM)
s.bind((host,port))
print 'Socket Bind Complete'
s.listen(10)
print 'Now Listening'
while 1:
conn, adr = s.accept()
print 'connected with' + adr[0] + ':' + str(adr[1])
data = conn.recv(50)
print data
if not data:
break
conn.sendall(data)
conn.close()
s.close()
The problem is that you're accepting a new connection each time through the loop, and only receiving from that connection once. The next time through the loop, your forget about that connection and accept a new one, which blocks until someone else connects.
If you just want to handle a single connection and quit, just move the accept outside the loop:
conn, adr = s.accept()
print 'connected with' + adr[0] + ':' + str(adr[1])
while True:
data = conn.recv(50)
print data
if not data:
break
conn.sendall(data)
conn.close()
s.close()
If you want to handle one connection at a time, but then wait for a new connection after you finish with the first, add an outer loop.
while True:
conn, adr = s.accept()
print 'connected with' + adr[0] + ':' + str(adr[1])
while True:
data = conn.recv(50)
print data
if not data:
break
conn.sendall(data)
conn.close()
s.close()
If you want to handle more than one connection at a time, as most servers do, you need some sort of asynchronous mechanism—select and nonblocking sockets, gevent, threads, whatever. For example:
def handle_client(conn, addr):
print 'connected with' + adr[0] + ':' + str(adr[1])
while True:
data = conn.recv(50)
print data
if not data:
break
conn.sendall(data)
conn.close()
client_threads = []
try:
while True:
conn, adr = s.accept()
client_thread = threading.Thread(target=handle_client, args=[conn, addr])
client_thread.start()
client_threads.append(client_thread)
finally:
s.close()
for client_thread in client_threads:
client_thread.join()
In any of these designs, you're probably better off using a with statement instead of explicit close calls and/or try/finally.