客户端未从服务器接收数据 python
Clients not receiving data from Server python
我是 Python 的新手。我正在编写一个服务器程序和一个客户端程序。在这里,服务器扮演着将数据分发给多个客户端的角色。它很好用。我的任务是使用 server.py 文件从服务器分发数据。每当有客户需要时,他只需在笔记本电脑上执行 clients.py 并获得结果。但是在这里,服务器开始分发数据。服务器使用的 ip 是 127.0.1.1。它没有使用网络提供的 ip。如何让它使用局域网提供的ip。当来自其他计算机的客户端执行 clients.py 文件时。它显示连接被拒绝错误。请注意,我们都连接在局域网中。如何解决,让客户端接收到数据。
这是示例客户端代码:
import socket
import os
from threading import Thread
import socket
import time
s = socket.socket()
host = '127.0.1.1'
port = 10016
print(host)
s.connect((host, port))
while True:
print(s.recv(1024))
s.close()
示例服务器代码:
import socket
import os
from threading import Thread
import thread
import threading
import time
import datetime
def listener(client, address):
print ("Accepted connection from: ", address)
with clients_lock:
clients.add(client)
try:
while True:
client.send(a)
time.sleep(2)
finally:
with clients_lock:
clients.remove(client)
client.close()
clients = set()
clients_lock = threading.Lock()
host = socket.gethostname()
port = 10016
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(3)
th = []
print ("Server is listening for connections...")
while True:
client, address = s.accept()
timestamp = datetime.datetime.now().strftime("%b %d %Y,%a, %I:%M:%S %p")
a = "Hi Steven!!!" + timestamp
th.append(Thread(target=listener, args = (client,address)).start())
s.close()
配置局域网提供的ip为client.py(局域网内的ip是这样的:192.168.122.33)
host = 'ip provided by LAN'
终于找到答案了
在“/etc/hosts”文件内容中,我将“127.0.1.1”映射到我的主机名的 IP 地址。这导致名称解析为 127.0.1.1。我评论了这一行。现在可以了。我局域网内的每个人都可以收到数据
服务器代码:
import socket
import os
from threading import Thread
import threading
import time
import datetime
def listener(client, address):
print ("Accepted connection from: ", address)
with clients_lock:
clients.add(client)
try:
while True:
client.send(a)
time.sleep(2)
finally:
with clients_lock:
clients.remove(client)
client.close()
clients = set()
clients_lock = threading.Lock()
host = socket.getfqdn() # it gets ip of lan
port = 10016
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(3)
th = []
print ("Server is listening for connections...")
while True:
client, address = s.accept()
timestamp = datetime.datetime.now().strftime("%b %d %Y,%a, %I:%M:%S %p")
a = ("Hi Steven!!!" + timestamp).encode()
th.append(Thread(target=listener, args = (client,address)).start())
s.close()
客户代码:
import socket
import os
import time
s = socket.socket()
host = '192.168.1.43' #my server ip
port = 10016
print(host)
print(port)
s.connect((host, port))
while True:
print((s.recv(1024)).decode())
s.close()
我是 Python 的新手。我正在编写一个服务器程序和一个客户端程序。在这里,服务器扮演着将数据分发给多个客户端的角色。它很好用。我的任务是使用 server.py 文件从服务器分发数据。每当有客户需要时,他只需在笔记本电脑上执行 clients.py 并获得结果。但是在这里,服务器开始分发数据。服务器使用的 ip 是 127.0.1.1。它没有使用网络提供的 ip。如何让它使用局域网提供的ip。当来自其他计算机的客户端执行 clients.py 文件时。它显示连接被拒绝错误。请注意,我们都连接在局域网中。如何解决,让客户端接收到数据。
这是示例客户端代码:
import socket
import os
from threading import Thread
import socket
import time
s = socket.socket()
host = '127.0.1.1'
port = 10016
print(host)
s.connect((host, port))
while True:
print(s.recv(1024))
s.close()
示例服务器代码:
import socket
import os
from threading import Thread
import thread
import threading
import time
import datetime
def listener(client, address):
print ("Accepted connection from: ", address)
with clients_lock:
clients.add(client)
try:
while True:
client.send(a)
time.sleep(2)
finally:
with clients_lock:
clients.remove(client)
client.close()
clients = set()
clients_lock = threading.Lock()
host = socket.gethostname()
port = 10016
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(3)
th = []
print ("Server is listening for connections...")
while True:
client, address = s.accept()
timestamp = datetime.datetime.now().strftime("%b %d %Y,%a, %I:%M:%S %p")
a = "Hi Steven!!!" + timestamp
th.append(Thread(target=listener, args = (client,address)).start())
s.close()
配置局域网提供的ip为client.py(局域网内的ip是这样的:192.168.122.33)
host = 'ip provided by LAN'
终于找到答案了
在“/etc/hosts”文件内容中,我将“127.0.1.1”映射到我的主机名的 IP 地址。这导致名称解析为 127.0.1.1。我评论了这一行。现在可以了。我局域网内的每个人都可以收到数据
服务器代码:
import socket
import os
from threading import Thread
import threading
import time
import datetime
def listener(client, address):
print ("Accepted connection from: ", address)
with clients_lock:
clients.add(client)
try:
while True:
client.send(a)
time.sleep(2)
finally:
with clients_lock:
clients.remove(client)
client.close()
clients = set()
clients_lock = threading.Lock()
host = socket.getfqdn() # it gets ip of lan
port = 10016
s = socket.socket()
s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
s.bind((host,port))
s.listen(3)
th = []
print ("Server is listening for connections...")
while True:
client, address = s.accept()
timestamp = datetime.datetime.now().strftime("%b %d %Y,%a, %I:%M:%S %p")
a = ("Hi Steven!!!" + timestamp).encode()
th.append(Thread(target=listener, args = (client,address)).start())
s.close()
客户代码:
import socket
import os
import time
s = socket.socket()
host = '192.168.1.43' #my server ip
port = 10016
print(host)
print(port)
s.connect((host, port))
while True:
print((s.recv(1024)).decode())
s.close()