I am building a simple Python server than will receive data from the client and print it on screen. This is my code so far.
SERVER CODE:
import socket
def server(interface, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
sock.bind((interface, port))
sock.listen(1)
print('Listening at', sock.getsockname())
while True:
sc, sockname = sock.accept()
print('We have accepted a connection from', sockname)
print(' Socket name:', sc.getsockname())
print(' Socket peer:', sc.getpeername())
if __name__ == '__main__':
server('0.0.0.0', 8000)
CLIENT CODE:
import socket
def client(host, port):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
print('Client has been assigned socket name', sock.getsockname())
if __name__ == '__main__':
client('localhost', 8000)
No matter how I tried to send data from the client to the server, it always errored out saying: Transport endpoint is not connected.
Try Using 127.0.0.1
or 192.168.1.X
(your private ip which you can find by using ipconfig
in cmd) instead of localhost, this worked for me
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.