python

python thread

1)

import thread

thread.start_new_thread(clientthread ,("Thread-1", s))

2)

from thread import *

conn, addr = s.accept()
print 'Connected with ' + addr[0] + ':' + str(addr[1])

#start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
start_new_thread(clientthread ,(conn,))

 

demo

How to Program UDP sockets in Python – Client and Server Code Example

https://www.binarytides.com/programming-udp-sockets-in-python/

import socket
import sys
import csv
import math
import re
import time
from thread import *
import thread

cnt = 1
a = [0.0]*3
sp = 0.0
#l = [0.0]*101
l = [0.0]*51
l[0] = sys.argv[2]
counter = 0

HOST = ''   # Symbolic name meaning all available interfaces
PORT = int(sys.argv[1]) # Arbitrary non-privileged port

s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
print 'Socket created'

#Bind socket to local host and port
try:
    s.bind((HOST, PORT))
except socket.error as msg:
    print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
    sys.exit()

print 'Socket bind complete'

#Start listening on socket
#s.listen(10)
#print 'Socket now listening'

#Function for handling connections. This will be used to create threads
def clientthread(threadName, conn):
    #Sending message to connected client
    #conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
    global counter
    counter+=1
    #infinite loop so that function do not terminate and thread do not end.
    while True:

        #Receiving from client
        data = conn.recv(1024)
        print >>sys.stderr, 'received "%s"' % data
        
        ll = re.findall(r"\d+\.?\d*", data)

        a[0] = float('%.6f' % float(ll[0]))/9.8
        a[1] = float('%.6f' % float(ll[1]))/9.8
        a[2] = float('%.6f' % float(ll[2]))/9.8

        sp = math.sqrt(math.pow(a[0],2) + math.pow(a[1],2) + math.pow(a[2],2))
        # print("sp:%10.6f" % sp)
        global cnt
        if cnt != 0:
            l[cnt] = float('%.6f' % sp)
        cnt += 1
        #if cnt > 100:
        if cnt > 50:
            print(cnt)
            print(l)
            cnt = 1
            with open(sys.argv[3]+str(counter)+".csv", "a") as file:
                    writer = csv.writer(file ,delimiter=',')
                    writer.writerow(l)


        reply = 'OK...' + data
        if not data: 
            break

        #conn.sendall(reply)

    #came out of loop
    conn.close()


thread.start_new_thread(clientthread ,("Thread-1", s))
#start_new_thread(clientthread ,(s,))

#now keep talking with the client
while 1:
    #wait to accept a connection - blocking call
    #conn, addr = s.accept()
    #print 'Connected with ' + addr[0] + ':' + str(addr[1])

    #start new thread takes 1st argument as a function name to be run, second is the tuple of arguments to the function.
    #start_new_thread(clientthread ,(conn,))
    #
    time.sleep(1)

s.close()

 

Python JSON

https://www.programiz.com/python-programming/json

 

使用Matplotlib绘制3D图形

https://paul.pub/matplotlib-3d-plotting/

17、Matplotlib 画动态图: animation模块的使用

https://blog.csdn.net/u013180339/article/details/77002254

16、json

# -*- coding:UTF-8 -*-

import json
import re

data = {"Fence":[{"Type":2,"OrganizationId":"qqq","StationId":"ppp","Id":"ooo","Name":"ttt"}]}

# json to str
data_str = json.dumps(data)
print(type(data_str))
print(data_str)


#str to dict
user_dict = json.loads(data_str)
tt = user_dict['Fence'][0]['Type']
print tt

 

15、python flask 登陆界面,表单

https://blog.csdn.net/aimill/article/details/81352684

14、python时间日期日历

https://www.runoob.com/python/python-date-time.html

13、python函数/参数、返回值

https://www.cnblogs.com/ilym/p/8310752.html

12、python线程同步

event

http://blog.51cto.com/freshair/1899372

queue

https://blog.csdn.net/qq_39304201/article/details/79654711

mutex

https://www.2cto.com/kf/201706/653087.html

 

11、python读取shell指令

#output = os.popen(message['data'])
#logs=output.readlines()
#for line in logs:
#    print line
 
return_code, logs = commands.getstatusoutput(message['data'])
print logs

 

10、python_version_checkout

楼主是Ubuntu14.04版本,一直以来想用Python3版本,但是又不能把自带的Python2版本卸载,据说卸载之后系统会不稳定。因此一直苦于Python2和Python3无法及时切换,导致软件弄混。后来终于找到一个方法可以完美切换。切换方法如下:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 100
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 150

然后再输入:

sudo update-alternatives --config python

根据数字选择你想要的Python版本。这时,Python及其对应的pip都跟着变成默认的了。

9、virtualenv

sudo apt-get install python-pip
sudo pip install virtualenv
virtualenv -p /usr/bin/python env
source env/bin/activate
pip --version
pip install --upgrade tensorflow

pip install -r requirements.txt

 

8、requirements.txt

pip freeze > requirements.txt

pip install -r requirements.txt

 

7、python读写shell

https://www.cnblogs.com/snow-backup/p/5035792.html

 

 

6、Ubuntu环境下python2和python3切换

https://blog.csdn.net/gan_player/article/details/72057966

 

5、python和c语言参数传递

1)c--->python
http://blog.csdn.net/JoeBlackzqq/article/details/10441017
http://blog.csdn.net/chenyulancn/article/details/8158168
2)python--->c
http://blog.csdn.net/mrcheny/article/details/79022973

 

4、python使用ctypes调用C/C++

http://blog.csdn.net/u012449363/article/details/75452374

 

3、使用python向C语言的链接库传递数组、结构体、指针类型的数据

http://blog.csdn.net/u012449363/article/details/76690540

 

2、Python Ctypes结构体指针处理(函数参数,函数返回)

本文演示了在python中调用C语言生成的动态库,返回结构体指针,并进行输出!

http://blog.csdn.net/joeblackzqq/article/details/10441017

1、浅谈 Python 程序和 C 程序的整合

https://www.ibm.com/developerworks/cn/linux/l-cn-pythonandc/

 

0、python读写json文件

https://www.jianshu.com/p/a4a7d75bf7d9

 

posted @ 2017-02-17 09:25  dong1  阅读(267)  评论(0编辑  收藏  举报