套接字 - 主机无效语法?蟒蛇

时间:2014-02-12 10:58:56

标签: python sockets networking python-3.x syntax

是的,所以我在一台机器上的两个程序之间进行简单的通信,虽然我打算将程序放在两台机器上并让它们在本地局域网上进行通信。我在client.py上的主机上获得了无效的语法

-Server.py

import socket

s = socket.socket()         
host = socket.gethostname() 
port = 12345                
s.bind((host, port))        

s.listen(5)                 
while True:
   c, addr = s.accept()     
   print ('Got connection from', addr)
   c.send("Thank you for connecting".encode())
   c.close()

client.py

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = 192.168.1.161  
port = 12345                # Reserve a port for your service.

s.connect((host, port))
print (s.recv(1024))
s.close                     # Close the socket when done

我正在使用Python 3.x

1 个答案:

答案 0 :(得分:3)

您应该引用主机字符串:

host = "192.168.1.161"