Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

function - Restart or cancel input() [Python]

I want to restart the input('CLIENT >> ') when the client recieves a message from the server and the same for the server (the server and client being python scripts in this case) client.py:

import socket

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

print('Connecting to ', host, port)
s.connect((host, port))

while True:
  msg = input('CLIENT >> ')
  s.send(msg.encode())
  msg = str(s.recv(1024))
  print('SERVER >> ', str(msg))

server.py:

import socket

s = socket.socket()
host = ''
port = 12345

print('Server started!')
print('Waiting for clients...')

s.bind((host, port))
s.listen(5)
c, addr = s.accept()
print('Got connection from', addr)

while True:
    recieved = c.recv(1024)
    print('
', addr, ' >> ', str(recieved))
    msg = input('SERVER >> ')
    c.send(msg.encode())

NOTES:

  • Using my laptop to run both of these scripts, I don't have an actual server in real life
  • Python Version: 3.8
  • OS: Windows 10
  • Editor: PyCharm Community Edition
question from:https://stackoverflow.com/questions/65871178/restart-or-cancel-input-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I don't understand what restarting input means... If you mean to show messages while 'writing messages' in the input then consider learning about threads, and spin off a thread to get messages from the server/client and print to the screen.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...