我有两个脚本server.py
和talk.py
server.py
应该听取talk.py
的连接和消费数据
它适用于localhost,即如果我在同一台机器上的不同终端运行server.py
和talk.py
...
a)talk.py
要求输入字符串b)server.py
使用a)中的字符串并将其打印在运行server.py
的终端中
就像我说的,脚本在本地机器Ubuntu上运行正常。
当我将server.py
部署到GCP VM实例时似乎运行正常但是,为什么我在本地机器上运行我的talk.py
代码以通过外部Web IP地址连接到server.py
套接字,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()
看起来像路由/安全问题而不是代码。这可能会有所帮助:How to port forward Google Compute Engine Instance?
[编辑]另一种资源:How to open a specific port such as 9090 in Google Compute Engine