python--Network Programming-----Socket code Example

Source: Internet
Author: User

First, the basic socket example

Service side:

1 ImportSocket2 3HOST ="'4PORT = 500075 6Sock_server =Socket.socket (socket.af_inet, socket. SOCK_STREAM)7 Sock_server.bind ((HOST, PORT))8 9Sock_server.listen (1)Tenconn, addr =sock_server.accept () One  A With Conn: -     Print('Connected by', addr) -      whileTrue: thedata = CONN.RECV (1024) -         if  notData: -              Break -Conn.sendall (data)

Client:

1 ImportSocket2 3HOST ='localhost'4PORT = 500075 6Client =Socket.socket (socket.af_inet, socket. SOCK_STREAM)7 Client.connect ((HOST, PORT))8Client.sendall (b'Hello, World')9 Tendata = CLIENT.RECV (1024) One  A Print('Received', data)

Run the server and then run the client, and the client will run the result:

1 Received b'Hello,world'

II. cyclic data transmission and receiving

Service side:

1 ImportSocket2 3HOST ="'4PORT = 500075 6Sock_server =Socket.socket (socket.af_inet, socket. SOCK_STREAM)7 Sock_server.bind ((HOST, PORT))8 9Sock_server.listen (1)Tenconn, addr =sock_server.accept () One  A With Conn: -     Print('Connected by', addr) -      whileTrue: thedata = CONN.RECV (1024) -         Print("Server recv:", Conn.getpeername (), Data.decode ()) -         if  notData: -              Break +Conn.sendall (data)

Client:

1 ImportSocket2 3HOST ='localhost'4PORT = 500075 6Client =Socket.socket (socket.af_inet, socket. SOCK_STREAM)7 Client.connect ((HOST, PORT))8 9  whileTrue:Tenmsg = input (">>>:"). Strip () One     ifLen (msg) = =0: A         Continue - Client.sendall (Msg.encode ()) -  thedata = CLIENT.RECV (1024) -  -     Print('Received', Data.decode ())

First run the server, run the client, and then enter the client in turn:

Hi, hello, learn python,

The client runs the result as:

1 >>>: Hi2Received hi3 >>>: Hello 4 Hello, Received . 5 >>>: Learn python6Received Learn python7 >>>:

The server run results are:

1 Connected by ('127.0.0.1', 57473)2 server recv: (' 127.0.0.1 ', 57473) Hi3 server recv: ('127.0.0.1', 57473) Hello  4 server recv: ('127.0.0.1', 57473) Learn Python

Three, simple chat software

Service side

1 ImportSocket2 3HOST ="'4PORT = 500075 6Sock_server =Socket.socket (socket.af_inet, socket. SOCK_STREAM)7 Sock_server.bind ((HOST, PORT))8 9Sock_server.listen (1)Tenconn, addr =sock_server.accept () One  A With Conn: -     Print('Connected by', addr) -      whileTrue: thedata = CONN.RECV (1024) -         Print("Server recv:", Conn.getpeername (), Data.decode ()) -         if  notData: -              Break +  -Response = input (">>>:"). Strip () + Conn.send (Response.encode ()) A         Print("Send to Alex:", response)

Client

1 ImportSocket2 3HOST ='localhost'4PORT = 500075 6Client =Socket.socket (socket.af_inet, socket. SOCK_STREAM)7 Client.connect ((HOST, PORT))8 9  whileTrue:Tenmsg = input (">>>:"). Strip () One     ifLen (msg) = =0: A         Continue - Client.sendall (Msg.encode ()) -  thedata = CLIENT.RECV (1024) -  -     Print('Received', Data.decode ())

Run the server first, then run the client,

The client runs the result as:

1 >>>: Hi2Received Hey3 >>>: strong 4 Received Alex 5 >>>: Egon in 6Received not 7 >>>: haha 8 Received haha 9 >>>:

The server run results are:

1Connected by ('127.0.0.1', 57682)2Server recv: ('127.0.0.1', 57682) Hi3>>>: Hey4 Send to Alex:hey5Server recv: ('127.0.0.1', 57682) Strong6>>>: Alex7 Send to Alex:alex8Server recv: ('127.0.0.1', 57682is Egon in?9>>>: NotTen Send to Alex: not in OneServer recv: ('127.0.0.1', 57682) haha A>>>: Haha -Send to Alex: haha

Can you see that the above code enables simple chat functions

python--Network Programming-----Socket code Example

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.