2015-01-11 续
本周开始Python的入门学习,跟进Learnpythonhardway的课程
Learnpythonhardway to ex4
Case: 作者要求在Linux系统下使用gedit,直接安装失败。update之后安装,失败。 Reboot 安装失败,类似当时安装VNC的时候,明天连香港的网络 尝试。
A: 初步怀疑是安装要求关闭一些软件,但是我已经在运行VNC程序。
内核之声:
<span style="font-family: Arial;"><span style="font-size:14px;">$ sudocat /book/kernel.img | aplay</span></span>
查看版本:
<span style="font-family:Arial;"><span style="font-size:14px;">$ cat/proc/version</span></span>
<span style="font-family:Arial;"><span style="font-size:14px;">$ cat /proc/cpuinfo</span></span>
2015-01-12
Case: 想设置连接公司双网络,更新gedit. 但是接入以后就会导致局域网挂掉。原因未知,可能是由于IP冲突,更谨慎的怀疑就是我的Pi已经被黑,植入了某 些程序,接入公司网络的时候触发了防火墙。IP污染的可能性很大。第一次造成wifi和eth都挂掉。之后重启交换机之类恢复。第二次造成eth挂掉。wifi 没事,暂时不知道原因。
A: 后来处理结果为公司交换机硬件升级,在之后的连接中未再出现这种情况。这次网络中断大概持续了2.5小时,不幸中的万幸就是当天工作符合尚可,没有 给同事带来什么困扰影响办公。在公司练习Pi的使用还是应该慎重使用。
interfaces内置设两个 ssid结果导致现有的也连接不上。也许是还没找对设置的路径,也许是Linux的编辑格式使然,加之引起断网的事件,最后恢复原样。eth0保持dhcp,因为在公司连接有线网口已经被固定了IP。wlan0 设置为家里的ssid,并且在路由端为无线网卡的mac指定了IP.
在后续安装gedit的时候依旧报错,排除网络原因,依然无法理解。初步判断是因为Debian或者说Raspbian系统不支持??虽然此分析不太现实,但是也不想再gedit上继续消耗时间,索性就在win电脑上用notepad ++编辑好,拷贝到nano编辑的文件里。在Pi上保存并运行。
Tips: 使用Putty的时候右键即为复制。
Ex5, %r stand for all types. R means Raw.
Q: Why does %r sometimes printthings with single-quotes when I wrote them with double-quotes?
A: Python is going to print the strings in the most efficient way it can, not replicate exactly the way you wrote them. This is perfectly fine since %r is used for debugging and inspection, soit's not necessary that it be pretty.
来自 <http://learnpythonthehardway.org/book/ex8.html>
Ex10
ESCAPE | WHAT IT DOES. |
\\ | Backslash () |
\' | Single-quote (') |
\" | Double-quote (") |
\a | ASCII bell (BEL) |
\b | ASCII backspace (BS) |
\f | ASCII formfeed (FF) |
\n | ASCII linefeed (LF) |
\N{name} | Character named name in the Unicode database (Unicode only) |
\r ASCII | Carriage Return (CR) |
\t ASCII | Horizontal Tab (TAB) |
\uxxxx | Character with 16-bit hex value xxxx (Unicode only) |
\Uxxxxxxxx | Character with 32-bit hex value xxxxxxxx (Unicode only) |
\v | ASCII vertical tab (VT) |
\ooo | Character with octal value ooo |
\xhh | Character with hex value hh |
来自 <http://learnpythonthehardway.org/book/ex10.html>
Ex13
Argv means the argument variable from subject = title.
Ex14
Add another argumentand use it in your script, the same way you did in the previous exercisewith first, second = ARGV.
来自 <http://learnpythonthehardway.org/book/ex14.html>
Ex15
Open( )and .read( )
Open means making a file an object, and this object could be opened twice or more.
2015-01-16
Ex16
Q:If you open the file with 'w' mode,then do you really need the target.truncate()? Read the documentation for Python's open function and see if that's true.
A: doc instruction: Open a file using the file() type, returns a file object. This is the preferred way to open a file. Thought the doc instruction cannot show me the answer. I got other info that the 'w' means overwrite this file in other words this file would be truncate automatically before input data. Another point of view, truncate could be set para to delete indicated data inside.
源文档 <http://learnpythonthehardway.org/book/ex16.html>
More:
open(filename, mode)
Thefirst argument is a string containing the filename. The second argument isanother string containing a few characters describing the way in which the filewill be used. mode can be 'r' when the file will only be read, 'w' for only writing (an existing file with the same name will beerased), and 'a' opens the file for appending; any data writtento the file is automatically added to the end. 'r+' opens the file for both reading and writing. The mode argument is optional; 'r' will be assumed if it’s omitted.
源文档 <https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files>
Fileobjects have some additional methods, such as isatty() and truncate() which are less frequently used; consult theLibrary Reference for a complete guide to file objects.
源文档 <https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files>
2015-01-13
Functionabout file
Close | File->save |
Read | Could be assigned to a var |
Readline | Just read one line of the file |
Truncate | Empties not to Delete |
Write('stuff') | Write "stuff" |
<span style="font-family:Arial;"><span style="font-size:14px;">$ pydoc raw_input</span></span>
<span style="font-size:14px;"><span style="font-family:Arial;">$ pydoc open </span>
</span>
- check function instruction, for more in ex12.
Ex18
(*arg) works like argv
Ex19
To avoid global var
Ex20
A file in Python is kind of like an old tape drive on a mainframe, or maybe a DVD player.It has a "read head," and you can "seek" this read head around the file to positions, then work with it there. Each time you do f.seek(0) you're moving to the start of the file. Each time you do f.readline() you're reading a line from the file, and moving the read head to right after the \n that ends that file. This will be explainedmore as you go on.
源文档 <http://learnpythonthehardway.org/book/ex20.html>
The readline() function returns the \n, and avoid adding double \n to every line.
Q:Why does seek(0) not set the current_line to 0?
A:First, the seek() function is dealing in bytes, not lines. The code seek(0) moves the file to the 0 byte (firstbyte) in the file. Second, current_line is just a variable and has no real connection to the file at all. We are manually incrementing it.
源文档 <http://learnpythonthehardway.org/book/ex20.html>
Ex25
Write a module, input ' help(ex25) ' to check the instruction of the module.
Ex26debug
Ex28
Q:Why does "test" and"test" return "test" or 1 and 1 return 1 insteadof True?
A:Python and many languages like to return one of the operands to their boolean expressions rather than just True or False. This means that if you did False and 1 you get the first operand (False) but if you do True and 1 your get the second (1).
Also Python use != and deprecated <>
源文档 <http://learnpythonthehardway.org/book/ex28.html>
Ex30
In python elif, deprecated 'else if'
树莓派各种用法合集: http://blog.csdn.net/xzknet/article/details/38989471
------------------------------------------------------------并行的实践分割线,以下笔记被归档于Project-Wechat--------------------------
本周开始实践Pi与微信的互动其中也遇到不少问题待解决。
Pi -Wechat互动
起因是经过搜索看到以下发帖,即使用树莓派完成家庭环境监控互动。
http://c.tieba.baidu.com/p/3232803694?pn=1
http://www.cnblogs.com/txw1958/p/weixin-Raspberry-Pi.html
分享的源代码 <<weixind.tar.gz>>
同Github备份, python的微信公共平台的sdk(用作接入验证):
https://github.com/kun945/weixinpy
源文档 <http://www.cnblogs.com/txw1958/p/weixin-Raspberry-Pi.html>
Yeelink,python sdk:
https://github.com/kun945/yeelink
源文档 <http://www.cnblogs.com/txw1958/p/weixin-Raspberry-Pi.html>
Pi +ardino(帖子作者控制程序的源代码):
https://github.com/kun945/weixind/blob/master/weixind.py
源文档 <http://www.cnblogs.com/txw1958/p/weixin-Raspberry-Pi.html>
微信开发者测试平台,sdk管理:
http://mp.weixin.qq.com/debug/cgi-bin/sandboxinfo?action=showinfo&t=sandbox/index
微信公众平台wiki: http://mp.weixin.qq.com/wiki/home/index.html
开始按步骤实作:
查找IP为公网IP 1xx.1xx.1xx.250 (wan口与 whatismyip.com查询结果一致)
路由器进行端口映射,微信公众平台只能使用80端口
该python程序的运行需要安装以下module
<span style="font-family:Arial;"><span style="font-size:14px;">$ Sudo apt-get install python-lxml</span></span>
微信公众平台测试号: 微信号 gxxxxxxxxxxxxxx6
appID:wx67xxxxxxxxxxb89
Appsecret:0xxxxxxxxxxxxxxxxxxxxxxxd
URL:http://1xx.1xx.1xx.2xx:80/weixind.py
Token: xxxxxxxx
发现无法执行 import web, 由于没有加载该module
查找网上的条目并没有解决问题,后来使用 sudo apt-get install web.py成功抓取!
但是其中还是不包含web module? 但是经过查询web.py与web module是同一个程序,无法理解为什么无法加载。
新建 ~/temp/test 按照说明nano新建weixind.py 并录入楼主的脚本,然后键入Token.
Case: ./weixind.py 80 --- No module named web
A:
<span style="font-family:Arial;"><span style="font-size:14px;">$ sudo apt-get install python-pip
$ sudo apt-get install python-lxml
$ sudo pip install web.py</span></span>
<span style="font-family:Arial;"><span style="font-size:14px;">~/temp/test $ sudo ./weixind.py 80</span></span>
成功显示 http://0.0.0.0:80/
但是公众号依旧失败。
Case: Socket error
A: 初步原因是由于80端口被占用,之前有成功运行过程序。reboot重新运行,再次判断是由于结束程序时使用的是ctrl+z指令,并未关闭运行的脚本,只是使其后台运行并返回命令行,若需要关闭占用80端口的程序应使用ctrl+C关闭。
微信一直提示配置失败,再之后查看说明,Token只能由英文和数字组成且不能超过32个字符,自己作死啊...(第一次使用的时候把Token当做密钥,所以随手乱输了34个字符,数字以及符号)
卡壳微信公众平台账号链接,一直提示配置失败。
查看本地80端口状态:
<span style="font-family:Arial;"><span style="font-size:14px;">$ netstat-a -n 80</span></span>
<span style="font-family:Arial;"><span style="font-size:14px;">$ netstat -natp |grep 80</span></span>
公网ip问题?? http://www.codelast.com/?p=5374
早上起床手机查验ip为 1xx.1xx.1xx.5 (只变化了最后一组)
判断是公网ip的问题。
花生壳- 树莓派映射问题:
http://www.eeboard.com/bbs/thread-9826-1-1.html
http://www.cnblogs.com/iusmile/archive/2013/03/30/2991139.html
http://blog.sina.com.cn/s/blog_aab347d2010166ei.html
http://blog.csdn.net/rk2900/article/details/8658312
求助: http://www.zhihu.com/question/21868759
Dnspod 实现 ddns动态域名解析 http://blog.lixin.me/archives/29709/
Nginxproxy setup: https://www.digitalocean.com/community/tutorials/how-to-configure-nginx-as-a-reverse-proxy-for-apache
http://www.cyberciti.biz/tips/using-nginx-as-reverse-proxy.html
公网穿透: http://blog.csdn.net/quqi99/article/details/25080381
http://www.zhihu.com/question/23484662/answer/24723400
晚上查看IP为: 1xx.1xx.1xx.160 (最后一组又变了)
理论上只能绕道花生壳了
花生壳注册填完身份信息可以免费获取一个二级域名: http://xxxxx.xicp.net
Q: Phddns-2.0.6.i386.tar.gz 安装失败
A: pi的系统跟debian的还是有区别,或者是因为i386与Arm构架之类的问题么?没 能理解,但是根据提示找到了另一个版本,安装成功 phddns- 2.0.5.19225.tar.gz
Q: 连接不上,
NIC bind success
defOnStatusChanged okConnecting
defOnStatusChanged errorConnectFailed
A: 排查中--
打开/etc/phlinux.conf.把区中的szHost的值改成phlinux3.oray.net就可 以了。第一次打开并配置的时候把登陆地址输入成了自己的二级域名 导致无法链接。
来自 <http://bbs.oray.com/thread-109554-1-1.html>
Pi没有默认安装 nslookup,若要使用该指令还需安装
<span style="font-family:Arial;"><span style="font-size:14px;">$ sudo apt-get install dnsutils</span></span>
理论上公网ip问题解决,但是依旧无法认证,怀疑是80端口被ISP封锁,但是目前还无法理解TCP协议构架以及具体端口的用途。所以只能从搜索如果绕过ISP对80端口的封锁进行检索。
阅读该文答案 http://www.zhihu.com/question/23484662 整理了一下思路
微信->godaddy域名->dnspod解析->路由映射->Pi
已经提问,等回复。
期间也请教了计算机的同学,一位建议我用tomcat确认树莓派的公网ip与80端口是否有效暴露。另一位则表示ISP封80端口无解。现在卡在这一步。
-----------------------------------------------------------------------------------------------