简体   繁体   中英

Fix “[Errno 11] Resource temporarily unavailable” in Python 2.x.x?

I have two scripts server.py & talk.py

server.py is supposed to listen to connections & consume data from talk.py

It works perfectly on localhost ie if I run server.py & talk.py in different terminals on the same machine...

a) talk.py asks for a string b) server.py consumes the string from a) & prints it in the terminal that is running server.py

Like I said, the scripts run ok on a local machine, Ubuntu.

When I deployed my server.py to a GCP VM instance it seems runs fine, however, why I ran my talk.py code in on the local machine to connect to the server.py socket via an external web IP address, talk.py give the error code

Traceback (most recent call last):
  File "talk.py", line 11, in <module>
    s.connect(('35.247.28.0', port))
  File "/usr/lib/python2.7/socket.py", line 228, in meth
    return getattr(self._sock,name)(*args)
socket.error: [Errno 11] Resource temporarily unavailable
#server.py

# first of all import the socket library
import socket               

# next create a socket object
s = socket.socket()         
print "Socket successfully created"

# reserve a port on your computer in our
# case it is 12345 but it can be anything
port = 12345               

# Next bind to the port
# we have not typed any ip in the ip field
# instead we have inputted an empty string
# this makes the server listen to requests 
# coming from other computers on the network
s.bind(('', port))        
print "socket binded to %s" %(port)

# put the socket into listening mode
s.listen(5)     
print "socket is listening"           

# a forever loop until we interrupt it or 
# an error occurs
while True:

   # Establish connection with client.
   c, addr = s.accept()     
   print 'Got connection from', addr

   # Get data from client
   """
   try:
       data = c.recv(1024)
   except IOError as e:
       print e
   """

   print data

   if not data:
     break

   # Send back reversed data to client   
   c.sendall(data)


   # send a thank you message to the client. 
   #c.send('\n Thank you for sending message!!!!')

   # Close the connection with the client
   c.close()
# talk.py
# first of all import the socket library
import socket               

# next create a socket object
s = socket.socket()         
print "Socket successfully created"

# reserve a port on your computer in our
# case it is 12345 but it can be anything
port = 12345               

# Next bind to the port
# we have not typed any ip in the ip field
# instead we have inputted an empty string
# this makes the server listen to requests 
# coming from other computers on the network
s.bind(('', port))        
print "socket binded to %s" %(port)

# put the socket into listening mode
s.listen(5)     
print "socket is listening"           

# a forever loop until we interrupt it or 
# an error occurs
while True:

   # Establish connection with client.
   c, addr = s.accept()     
   print 'Got connection from', addr

   print data

   if not data:
     break

   # Send back reversed data to client   
   c.sendall(data)


   # send a thank you message to the client. 
   #c.send('\n Thank you for sending message!!!!')

   # Close the connection with the client
   c.close()

Seems like a routing/security issue rather than something with your code. This might help: How to port forward Google Compute Engine Instance?

[edit] Another resource: How to open a specific port such as 9090 in Google Compute Engine

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM