如何在python中从客户端向服务器发送消息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37227176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 19:05:05  来源:igfitidea点击:

How to send a message from client to server in python

pythonnetworkingserverclient

提问by Stamatis Papadopoulos

I'm reading two programs in Python 2.7.10 with client and server. How can I modify these programs in order to send a message from client to server?

我正在使用客户端和服务器阅读 Python 2.7.10 中的两个程序。如何修改这些程序以将消息从客户端发送到服务器?

server.py:

服务器.py:

#!/usr/bin/python           # This is server.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 12345                # Reserve a port for your service.
s.bind((host, port))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   c.send('Thank you for connecting')
   c.close()                # Close the connection

client.py:

客户端.py:

#!/usr/bin/python           # This is client.py file

import socket               # Import socket module

s = socket.socket()         # Create a socket object
host = socket.gethostname() # Get local machine name
port = 80              # Reserve a port for your service.

s.connect((host, port))
print s.recv(1024)
s.close                     # Close the socket when done

回答by Daniel

TCP sockets are bi-directional. So, after connection, there is no difference between server and client, you only have two ends of a stream:

TCP 套接字是双向的。因此,连接后,服务器和客户端之间没有区别,您只有一个流的两端:

import socket               # Import socket module

s = socket.socket()         # Create a socket object
s.bind(('0.0.0.0', 12345))        # Bind to the port

s.listen(5)                 # Now wait for client connection.
while True:
   c, addr = s.accept()     # Establish connection with client.
   print 'Got connection from', addr
   print c.recv(1024)
   c.close()                # Close the connection

and the client:

和客户:

import socket               # Import socket module

s = socket.socket()         # Create a socket object
s.connect(('localhost', 12345))
s.sendall('Here I am!')
s.close()                     # Close the socket when done

回答by Hari

The above answer throws an error: TypeError: a bytes-like object is required, not 'str'However, the following code worked for me:

上面的答案会引发错误:TypeError: a bytes-like object is required, not 'str'但是,以下代码对我有用:

server.py

服务器.py

import socket
import sys

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
port = 3125
s.bind(('0.0.0.0', port))
print ('Socket binded to port 3125')
s.listen(3)
print ('socket is listening')

while True:
    c, addr = s.accept()
    print ('Got connection from ', addr)
    print (c.recv(1024))
    c.close()

client.py:

客户端.py:

import socket

s = socket.socket()
port = 3125
s.connect(('localhost', port))
z = 'Your string'
s.sendall(z.encode())    
s.close()