2014-02-12 21 views
0

對,所以我在一臺機器上的兩個程序之間做了一個簡單的通信器,雖然我打算把這個程序放在兩臺機器上並讓它們在本地局域網上通信。我在client.py套接字 - 主機無效的語法? python

-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

In additi不要引用'host'字符串,你實際上不會調用* s.close'方法。 's.close()'是一個調用。 –

+0

謝謝,我爲什麼困惑的原因是你爲什麼不把端口號放在引號中? – Coder77

+0

因爲它是一個int文字;它是一個直的自然數。另一方面,IP地址不是Python中的字面值。 –

回答

3

你應該引用主機字符串:

host = "192.168.1.161"