我的 HTTP 伺服器(CentOS 7)上有一個 python 腳本,負責回應客戶端瀏覽器,它在 bash 中工作正常,但是當我嘗試透過 cgi 存取它時,它只會拋出一個錯誤。它在 Ubuntu 上運作良好(透過 HTTP 和 shell)腳本是:
import socket
HOST = '127.0.0.1'
PORT = 4345
print("Content-type: text/html; charset=utf-8\n\n")
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((HOST, PORT))
query = 'foobar'
sock.sendall(bytes(query, 'utf-8'))
buffer = sock.recv(1024)
responce = str(buffer, "UTF-8")
print(responce)
當我透過 CGI 運行腳本時遇到的錯誤是:
PermissionError: [Errno 13] Permission denied
Traceback (most recent call last):
File "/var/www/cgi-bin/script.py", line 8, in <module>
sock.connect((HOST, PORT))
PermissionError: [Errno 13] Permission denied
'getsebool -a | 的輸出grep httpd' 是:
httpd_anon_write --> off
httpd_builtin_scripting --> on
httpd_can_check_spam --> off
httpd_can_connect_ftp --> off
httpd_can_connect_ldap --> off
httpd_can_connect_mythtv --> off
httpd_can_connect_zabbix --> off
httpd_can_network_connect --> off
httpd_can_network_connect_cobbler --> off
httpd_can_network_connect_db --> off
httpd_can_network_memcache --> off
httpd_can_network_relay --> off
httpd_can_sendmail --> off
httpd_dbus_avahi --> off
httpd_dbus_sssd --> off
httpd_dontaudit_search_dirs --> off
httpd_enable_cgi --> on
httpd_enable_ftp_server --> off
httpd_enable_homedirs --> off
httpd_execmem --> off
httpd_graceful_shutdown --> on
httpd_manage_ipa --> off
httpd_mod_auth_ntlm_winbind --> off
httpd_mod_auth_pam --> off
httpd_read_user_content --> off
httpd_run_ipa --> off
httpd_run_preupgrade --> off
httpd_run_stickshift --> off
httpd_serve_cobbler_files --> off
httpd_setrlimit --> off
httpd_ssi_exec --> off
httpd_sys_script_anon_write --> off
httpd_tmp_exec --> off
httpd_tty_comm --> off
httpd_unified --> off
httpd_use_cifs --> off
httpd_use_fusefs --> off
httpd_use_gpg --> off
httpd_use_nfs --> off
httpd_use_openstack --> off
httpd_use_sasl --> off
httpd_verify_dns --> off
答案1
解決方案是允許 httpd 連接到網路。我剛開始httpd_can_network_connect
使用on
,setsebool
它就開始工作了。請注意,要使解決方案永久存在,您-P
還需要使用開關。