У меня есть два сценария server.py
& talk.py
server.py
должен прослушивать соединения и использовать данные из talk.py
Он отлично работает на localhost, т.е. если я запускаю server.py
& talk.py
в разных терминалах на одной машине ...
а) talk.py
просит строку
б) server.py
получает строку из а) и печатает ее в терминале, на котором работает server.py
Как я уже сказал, сценарии работают нормально на локальной машине, Ubuntu.
Когда я развернул server.py
на экземпляре виртуальной машины GCP, он, кажется, работает нормально,
однако, почему я запустил свой код talk.py
на локальном компьютере для подключения к сокету server.py
через внешний веб-IP-адрес, talk.py
выдает код ошибки
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()