Posts

Showing posts with the label Python3

PYTHON UnicodeEncodeError: 'ascii' codec can't encode character

import io,sys sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')

Python3 + Django + uWSGI + Nginx On FreeBSD

1. uWSGI Installation. $ sudo pip-3.6 install uwsgi Collecting uwsgi Downloading uwsgi-2.0.17.tar.gz (798kB) 100% |################################| 798kB 709kB/s Installing collected packages: uwsgi Running setup.py install for uwsgi ... done Successfully installed uwsgi-2.0.17 1.1 Basic test Let’s start with a simple “Hello World” example: def application(env, start_response): start_response('200 OK', [('Content-Type','text/html')]) return [b"Hello World"] As you can see, it is composed of a single Python function. It is called “application” as this is the default function that the uWSGI Python loader will search for (but you can obviously customize it). 1.2 Now start uWSGI to run an HTTP server/router passing requests to your WSGI application: uwsgi --http :9090 --wsgi-file foobar.py 2.Installing Django. Install Django into your virtualenv , create a new project $ mkproject django $ pip install Django Collecting Django ...

[FreeBSD] Installing and using Python's virtualenv using Python3

1. Installing Python3. $ sudo pkg install python36 Updating FreeBSD repository catalogue... FreeBSD repository is up to date. All repositories are up to date. The following 2 package(s) will be affected (of 0 checked): New packages to be INSTALLED: python36: 3.6.4 libffi: 3.2.1_2 Number of packages to be installed: 2 The process will require 103 MiB more space. 15 MiB to be downloaded. Proceed with this action? [y/N]: y [1/2] Fetching python36-3.6.4.txz: 100% 15 MiB 15.5MB/s 00:01 [2/2] Fetching libffi-3.2.1_2.txz: 100% 34 KiB 35.3kB/s 00:01 Checking integrity... done (0 conflicting) [1/2] Installing libffi-3.2.1_2... [1/2] Extracting libffi-3.2.1_2: 100% [2/2] Installing python36-3.6.4... Extracting python36-3.6.4: 100% Message from python36-3.6.4: =========================================================================== Note that some standard Python modules are provided as separate ports as they require additional dependencies. They are availabl...

[Python] Using Python to get weather data

Here's a simple Python code snippet for finding the weather. 1. #!/usr/bin/python3 # -*- coding: utf-8 -*- import urllib.request from contextlib import closing import xml.etree.ElementTree as ET rssurl = 'https://rss-weather.yahoo.co.jp/rss/days/4410.xml' with closing(urllib.request.urlopen(rssurl)) as res: xml = res.read() tree = ET.fromstring(xml) root = tree.findall('.') for item in root[0].iter('item'): title = item.find('title').text print(title[:title.index(' - Y')]) 2. import requests import json def get_weather(): url = 'http://weather.livedoor.com/forecast/webservice/json/v1' payload = {'city': '130010'} data = requests.get(url, params = payload).json() #resp = urllib2.urlopen('http://weather.livedoor.com/forecast/webservice/json/v1?city=%s'%citycode).read() #resp = json.loads(resp) print(data['description']['text']) ...