我正在尝试为一个仅在某些地方不提供Internet的城市制作测试程序,所以我想制作一个可以将数据分发到所有ppl的程序,Internet已连接到所有房屋,但要使用和退出城镇局域网连接并进入互联网,您需要付出很多。 但是我在尝试研究该代码的过程中遇到了困难,但发现几乎没有进展 过去使用过的这段代码可能会导致更新毁了它?
main.py(服务器):
from multiprocessing import Process
import socket
import time
ip = socket.gethostname()
port = 8080
def send(csock,data):
time.sleep(1)
csock.send(str(data).encode("utf-8"))
print("Done!")
print("Disconnecting!")
csock.close()
lsock = socket.socket()
lsock.bind((ip, port))
lsock.listen(1)
while True:
print("Ready!")
csock, addr = lsock.accept()
print("\nGot connection from {}\n".format(addr))
csock.send(str("Connected!").encode("utf-8"))
print("Connected!")
csock.send(str("Downloading data...").encode("utf-8"))
print("Uploading data...")
csock.send(str("\n\n").encode("utf-8"))
print("\n\n")
f = open("data.txt", "r")
data = f.read()
f.close()
print(data)
subs = Process(target=send, args=(csock,data, ))
subs.start()
csock.close()
Client.py:
import socket
ip = socket.gethostname()
port = 8080
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((ip,port))
while True:
mess = client.recv(1024).decode("utf-8")
if(mess):
print(mess)
data.txt:
Hello, this is a test!
控制台和错误:
Ready!
Got connection from ('192.168.100.84', 53205)
Connected!
Uploading data...
Hello, this is a test!
Ready!
Traceback (most recent call last):
File "<string>", line 1, in <module>
File "C:\Program Files (x86)\Python38-32\lib\multiprocessing\spawn.py", line 116, in spawn_main
exitcode = _main(fd, parent_sentinel)
File "C:\Program Files (x86)\Python38-32\lib\multiprocessing\spawn.py", line 125, in _main
prepare(preparation_data)
File "C:\Program Files (x86)\Python38-32\lib\multiprocessing\spawn.py", line 236, in prepare
_fixup_main_from_path(data['init_main_from_path'])
File "C:\Program Files (x86)\Python38-32\lib\multiprocessing\spawn.py", line 287, in
_fixup_main_from_path
main_content = runpy.run_path(main_path,
File "C:\Program Files (x86)\Python38-32\lib\runpy.py", line 265, in run_path
return _run_module_code(code, init_globals, run_name,
File "C:\Program Files (x86)\Python38-32\lib\runpy.py", line 97, in _run_module_code
_run_code(code, mod_globals, init_globals,
File "C:\Program Files (x86)\Python38-32\lib\runpy.py", line 87, in _run_code
exec(code, run_globals)
File "C:\Users\TNT\PycharmProjects\NEWS\main.py", line 16, in <module>
lsock.bind((ip, port))
OSError: [WinError 10048] Only one usage of each socket address (protocol/network address/port) is normally permitted