所以我一直在尝试将消息从我的虚拟机(服务器)传递到我的主机(客户端)我注意到我能够让我的服务器听我正在听IP:127.0 .1.1:端口1234。
服务器正在运行此代码,它没有给我任何编译错误 服务器再次在我的虚拟机(Ubuntu16_04)上运行。
Server_Script
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = socket.gethostbyname(socket.gethostname())
port = 1234
address=(ip,port)
server.bind(address)
server.listen(1)
print "[*] Started listening on ",ip,":",port
client.addr= server.accept()
print "[*] Got a connection from ",addr[0],":",addr[1]
while True:
data = client.recv(1024)
print "[*] Received '",data,"' from the client"
print " Processing data"
if(data--"Hello server"):
client.send("Hello client")
print " Processing done. \n[*] Reply sent"
elif(data=="disconnect"):
client.send("Goodbye")
client.close()
break
else:
client.send("Invalid data")
print " Processing done, Invalid data. \n[*] Reply sent"
但是当我尝试通过我的主机连接到服务器时,我收到此错误
> >>> import socket
>>> client=socket.socket()
>>> client.connect(('127.0.1.1',1234))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\PYTHON27\lib\socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10061] No connection could be made because the target machine actively refused it
这是我从主机上的Windows PowerShell收到的错误,客户端
我的计算机上的防火墙完全关闭了,我刚刚完成了本教程,试图设置一个静态IP,看看我是否可以让我的系统进行通信,但没有什么能真正起作用
我也将此用于端口转发问题,认为这可能与它有关但仍然没有运气(注意我使用了确切的静态IP地址作为指导基本上在重新尝试之前一步一步地完成了这些过程通过套接字连接计算机。)
如果有人知道我能做些什么才能让这个连接起作用我会非常感激!
这是我使用的代码,它通过powershell“python 2.7.14”给我一条错误消息
我尝试过脚本提供的IP,我尝试了设置静态IP时提供的IP
最后,我确定会在某个时候要求这个,所以我会把它放在这里,这基本上是我想要作为服务器的VM的ifconfig
enp0s3 Link encap:Ethernet HWaddr 08:00:27:74:4c:f0
inet addr:192.168.56.56 Bcast:192.168.56.255 Mask:255.255.255.0
inet6 addr: fe80::c378:fdbb:a955:5826/64 Scope:Link
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:49 errors:0 dropped:0 overruns:0 frame:0
TX packets:134 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:5620 (5.6 KB) TX bytes:14439 (14.4 KB)
enp0s8 Link encap:Ethernet HWaddr 08:00:27:42:79:85
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:4 errors:0 dropped:0 overruns:0 frame:0
TX packets:123 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:640 (640.0 B) TX bytes:20585 (20.5 KB)
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::1/128 Scope:Host
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:252 errors:0 dropped:0 overruns:0 frame:0
TX packets:252 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:19341 (19.3 KB) TX bytes:19341 (19.3 KB)
------------------------------ UPDATE ---------------- -------------------------
我已将原始服务器代码修改为不绑定到localhost(如建议),以及尝试通过适当的IP连接到服务器,我现在遇到了不同的错误代码,对我的任何建议可能搞砸了! :)
client.connect(('192.168.56.56',1234))
File "<stdin>", line 1, in <module>
File "C:\PYTHON27\lib\socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 10060] A connection attempt failed because the connected party did not properly respond after a period of time, or established connection failed because
connected host has failed to respond
答案 0 :(得分:0)
试试这个:
import socket
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
ip = ''
port = 1234
address=(ip,port)
server.bind(address)
server.listen(1)
print "[*] Started listening on ",ip,":",port
n = 0
while n < 1:
client.addr= server.accept()
n += 1
print "[*] Got a connection from ",addr[0],":",addr[1]
## stuff