Hướng dẫn how do i get client messages from server in python? - làm cách nào để lấy tin nhắn khách từ máy chủ trong python?

Tôi đang đọc hai chương trình trong Python 2.7.10 với máy khách và máy chủ. Làm thế nào tôi có thể sửa đổi các chương trình này để gửi tin nhắn từ máy khách đến máy chủ?

server.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:

#!/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

Hướng dẫn how do i get client messages from server in python? - làm cách nào để lấy tin nhắn khách từ máy chủ trong python?

Khi được hỏi ngày 14 tháng 5 năm 2016 lúc 13:20May 14, 2016 at 13:20

Ổ cắm TCP là hai chiều. Vì vậy, sau khi kết nối, không có sự khác biệt giữa máy chủ và máy khách, bạn chỉ có hai đầu của luồng:

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

Và khách hàng:

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

Đã trả lời ngày 14 tháng 5 năm 2016 lúc 13:36May 14, 2016 at 13:36

Câu trả lời trên đưa ra một lỗi: TypeError: a bytes-like object is required, not 'str' Tuy nhiên, mã sau đây đã hoạt động cho tôi:

server.py

import socket

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:

import socket

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

Hướng dẫn how do i get client messages from server in python? - làm cách nào để lấy tin nhắn khách từ máy chủ trong python?

Đã trả lời ngày 29 tháng 11 năm 2018 lúc 7:43Nov 29, 2018 at 7:43

Hari HariHari

711 Huy hiệu bạc3 Huy hiệu đồng1 silver badge3 bronze badges

1

Xem thảo luận

Cải thiện bài viết

Lưu bài viết

  • Đọc
  • Bàn luận
  • Xem thảo luận

    Cải thiện bài viết

    Lưu bài viết

    Đọc
    Socket programming is started by importing the socket library and making a simple socket. 
     

    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

    Bàn luậnAF_INET and the second one is SOCK_STREAM. AF_INET refers to the address family ipv4. The SOCK_STREAM means connection-oriented TCP protocol.
    Note: For more information, refer to Socket Programming in Python
    Now we can connect to a server using Server:
    A Server is a program that provides service to other computers on the network or Internet. Similarly, a client is a program that receives services from the server. When a server wants to communicate with a client, there is a need for a socket. A socket is a point of connection between the server and the client.
    TCP/IP server program that sends message to the client. 
     

    Python3

    import socket

    #!/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
    
    0____11
    #!/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
    
    2

    #!/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
    
    3
    #!/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
    
    1
    #!/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
    
    5

    #!/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
    
    6
    #!/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
    
    1
    #!/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
    
    8

    #!/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
    
    9
    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
    
    0

    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
    
    1

    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
    
    2
    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
    
    3
    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
    
    4

    Lập trình ổ cắm là một cách kết nối hai nút trên mạng để giao tiếp với nhau. Một ổ cắm (nút) lắng nghe trên một cổng cụ thể tại IP, trong khi ổ cắm khác đưa ra sang bên kia để tạo thành kết nối. Máy chủ tạo thành ổ cắm người nghe trong khi máy khách tiếp cận với máy chủ.Socket Lập trình được khởi động bằng cách nhập thư viện ổ cắm và tạo một ổ cắm đơn giản. & NBSP; & NBSP;

    Ở đây chúng tôi đã thực hiện một ví dụ về ổ cắm và vượt qua nó hai tham số. Tham số đầu tiên là AF_Inet và cái thứ hai là sock_stream. AF_Inet đề cập đến địa chỉ IPv4 gia đình. Sock_Stream có nghĩa là giao thức TCP định hướng kết nối. Tương tự, khách hàng là một chương trình nhận dịch vụ từ máy chủ. Khi một máy chủ muốn giao tiếp với máy khách, cần có ổ cắm. Một ổ cắm là một điểm kết nối giữa máy chủ và chương trình máy chủ khách.TCP/IP gửi tin nhắn đến máy khách. & NBSP; & NBSP;

    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
    
    4

    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
    
    5
    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
    
    6

    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
    
    5
    #!/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
    
    1
    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
    
    7

    import socket
    
    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()
    
    0

    import socket
    
    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()
    
    1

    Chương trình máy chủ TCP/IP nhận tin nhắn từ Máy chủ. & NBSP; & NBSP;
     

    Python3

    import socket

    #!/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
    
    0____11
    #!/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
    
    2

    #!/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
    
    3
    #!/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
    
    1
    #!/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
    
    5

    #!/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
    
    6
    #!/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
    
    1
    #!/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
    
    8

    #!/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
    
    9
    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
    
    0

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

    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
    
    7
    #!/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
    
    1
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    0
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    1
    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
    
    4

    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    3
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    4

    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    5
    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
    
    8
    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
    
    9
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    8
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    9 TypeError: a bytes-like object is required, not 'str'0

    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    5
    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
    
    7
    #!/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
    
    1
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    0
    import socket
    s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    1
    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
    
    4

    TypeError: a bytes-like object is required, not 'str'7

    Lưu ý: Mở trong hai Windows DOS riêng biệt và đầu tiên thực thi máy chủ, sau đó thực thi Client.Output của máy chủ: & nbsp; Open In Two Separate DOS Windows And First Execute server, then Execute client.
    Output of Server:
     

    Hướng dẫn how do i get client messages from server in python? - làm cách nào để lấy tin nhắn khách từ máy chủ trong python?

    Đầu ra của máy khách: & nbsp; & nbsp; 
     

    Hướng dẫn how do i get client messages from server in python? - làm cách nào để lấy tin nhắn khách từ máy chủ trong python?


    Làm cách nào để gửi tin nhắn từ máy chủ đến máy khách trong Python?

    Làm thế nào để bạn gửi tin nhắn cho khách hàng từ một máy chủ trong Python ?..
    Nhập ổ cắm ..
    Nhập SYS ..
    Host = ''.
    Cổng = 9000 ..
    s = ổ cắm. Ổ cắm (ổ cắm. AF_Inet, ổ cắm. Sock_Stream).
    In 'Ổ cắm được tạo'.
    S. BIND ((máy chủ, cổng)).

    Làm cách nào để gửi dữ liệu từ máy chủ đến máy khách trong Python?

    Overview:..
    Phương thức gửi () của lớp ổ cắm của Python được sử dụng để gửi dữ liệu từ ổ cắm này sang ổ cắm khác ..
    Phương thức gửi () chỉ có thể được sử dụng với một ổ cắm được kết nối. ....
    Phương thức gửi () có thể được sử dụng để gửi dữ liệu từ ổ cắm khách hàng dựa trên TCP đến ổ cắm được kết nối với máy khách dựa trên TCP ở phía máy chủ và ngược lại ..

    Làm thế nào để Python kết nối với máy chủ và máy khách?

    Sau đó, để tạo kết nối giữa máy chủ khách hàng, bạn sẽ cần sử dụng phương thức Connect () bằng cách chỉ định (host, port).Ở đây, khách hàng muốn nhận một số thông tin từ máy chủ và đối với điều này, bạn cần sử dụng phương thức recv () và thông tin được lưu trữ trong một biến số biến khác.use the connect() method by specifying (host, port). Here, the client wants to receive some information from the server and for this, you need to use the recv() method and the information is stored in another variable msg.

    Làm thế nào để bạn gửi và nhận tin nhắn trong Python?

    Đăng ký - hoặc đăng nhập vào - Twilio ..
    Cài đặt Twilio CLI ..
    Nhận số điện thoại ..
    Cài đặt Python và Thư viện trợ giúp Twilio ..
    Gửi một SMS bên ngoài với Python ..
    Thiết lập dịch vụ nhắn tin Twilio ..
    Tạo dịch vụ nhắn tin với số điện thoại của bạn ..
    Gửi SMS từ dịch vụ nhắn tin của bạn ..