返回顶部

收藏

Python的socket编程

更多

目前处在学习python的阶段,昨天看到了python的socket模块,分别实现TCP、UDP时间戳回显。 1、tcp通信server和client代码

# tcpServer.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

from socket import *
from time import ctime

HOST = ''
PORT = 21156
BUFSIZE = 1024
ADDR = (HOST,PORT)

tcpServerSock = socket(AF_INET,SOCK_STREAM)
tcpServerSock.bind(ADDR)
tcpServerSock.listen(5)

while True:
    print 'waiting for connection...'
    tcpClientSock ,addr = tcpServerSock.accept()
    print '...connected from:',addr

    while True:
        data=tcpClientSock.recv(BUFSIZE)
        if not data:
            break
        tcpClientSock.send('[%s] %s' % (ctime(),data))

        tcpClientSock.close()
tcpServerSock.close
``````python
# tcpClient.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

from socket import *

HOST = 'localhost'
PORT = 21156
BUFSIZE = 1024
ADDR = (HOST,PORT)

tcpClientSock = socket(AF_INET,SOCK_STREAM)
tcpClientSock.connect(ADDR)

while True:
    data = raw_input('>')
    if not data:
        break
    tcpClientSock.send(data)
    data = tcpClientSock.recv(BUFSIZE)
    if not data:
        break
    print data

tcpClientSock.close()

2、udp通信server和client代码

#udpServer.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

from socket import *
from time import ctime

HOST = ''
PORT = 21156
BUFSIZE = 1024
ADDR = (HOST,PORT)

udpServerSock = socket(AF_INET,SOCK_DGRAM)
udpServerSock.bind(ADDR)

while True:
    print 'waiting for message...'
    data ,addr = udpServerSock.recvfrom(BUFSIZE)
    udpServerSock.sendto('[%s] %s' % (ctime(),data), addr)
    print '...received from and returned to:',addr

        tcpClientSock.close()
tcpServerSock.close
``````python
#udpClient.py
#!/usr/bin/python
# -*- coding: utf-8 -*-

from socket import *
# from time import ctime

HOST = 'localhost'
PORT = 21156
BUFSIZE = 1024
ADDR = (HOST,PORT)

udpClientSock = socket(AF_INET,SOCK_DGRAM)

while True:
    data = raw_input('>')
    if not data:
        break
    udpClientSock.sendto(data,ADDR)
    data,ADDR = udpClientSock.recvfrom(BUFSIZE)
    if not data:
        break
    print data

udpClientSock.close()

标签:python

收藏

2人收藏

支持

1

反对

0

评论

  • What is Python?

    Python is an object-oriented language, which means that it can model real-world objects. It is also dynamically-typed because it carries out type-checking at runtime. It does so to make sure that the type of a construct matches what we expect in that context. The distinctive feature about Python is that it is an interpreted language. The Python IDLE (Integrated Development Environment) executes instructions a line at a time.

    Etymology states that Guido van Rossum named it after the comedy group Monty Python. That is why the metasyntactic variables used here are ‘spam’ and ‘eggs’ instead of ‘foo’ and ‘bar’. Unlike as expected, it does not refer to the reptile species. A lot of implementations today run version 2.x, but the future belongs to Python 3.x. It is also called ‘Python 3000’ or ‘Py3K’. CPython, written in C, is the most common implementation of Python. It compiles a Python program into intermediate bytecode.

    Apart from the constructs that Python provides, you can use the PyPI (Python Package Index). It is a repository of third-party modules, you can install it using a program called pip. Run the following command in Command Prompt:

    pip install library_name

    Python History

    Python programming language was conceived in the late 1980s and was named for the BBC TV show Monty Python’s Flying Circus. Guido van Rossum started python implementation at CWI in the Netherlands in December 1989. This was a successor to the ABC (programming language) which was capable of exception handling and interfacing with the Amoeba operating system.

    On October 16, 2000, python 2.0 release was there and it had many major new features, that includes cycle-detecting garbage collector for memory management and support for Unicode.

    Next version of Python 3.0 was released on December 3, 2008.

    Architecture

    This is the architecture of a CPython Compiler/Interpreter.

    a. Parser

    It uses the source code to generate an abstract syntax tree.

    b. Compiler

    It turns the abstract syntax tree into Python bytecode.

    c. Interpreter

    It executes the code line by line.

    to learn more components of python programming language. https://data-flair.training/blogs/python-tutorial/

    回复   |   dataflair008 发表于 2018-03-07 13:11:22

发表评论