<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Li</title>
    <description>The latest articles on DEV Community by Li (@socrateslee).</description>
    <link>https://dev.to/socrateslee</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F33748%2Fec089822-1ddb-4ef9-a980-f294faa2d8a0.png</url>
      <title>DEV Community: Li</title>
      <link>https://dev.to/socrateslee</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/socrateslee"/>
    <language>en</language>
    <item>
      <title>Use proxychains in WSL</title>
      <dc:creator>Li</dc:creator>
      <pubDate>Sat, 23 Dec 2023 15:49:32 +0000</pubDate>
      <link>https://dev.to/socrateslee/use-proxychains-in-wsl-3199</link>
      <guid>https://dev.to/socrateslee/use-proxychains-in-wsl-3199</guid>
      <description>&lt;p&gt;A WSL distro changes its IP and gateway address every time it restarts, so using proxychains would be a painful experience because the proxychains config file can not use domain names. The proxy runs on the host, forwarding traffic to a remote server.&lt;/p&gt;

&lt;p&gt;So a script to dynamically generate proxychains config file would be helpful.&lt;/p&gt;


&lt;div class="ltag_gist-liquid-tag"&gt;
  
&lt;/div&gt;


</description>
    </item>
    <item>
      <title>Finally, I figure out how to do shift + ins on HP laptop</title>
      <dc:creator>Li</dc:creator>
      <pubDate>Fri, 10 Mar 2023 15:34:19 +0000</pubDate>
      <link>https://dev.to/socrateslee/finally-i-figure-out-how-to-do-shift-ins-on-hp-laptop-440p</link>
      <guid>https://dev.to/socrateslee/finally-i-figure-out-how-to-do-shift-ins-on-hp-laptop-440p</guid>
      <description>&lt;p&gt;HP ELITEBOOK laptop combines F10 and insert keys together. It will kill you if you just want to do a shift + ins for pasting something in terminals. Fn + F10, Win + F10, shift + Win + F10, … , none of those work.&lt;/p&gt;

&lt;p&gt;Finally, I figure out how to do a shift + ins. Just&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;right shift&lt;/strong&gt; + &lt;strong&gt;Fn&lt;/strong&gt; + &lt;strong&gt;E&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Note that left shift is combined with fn lock, so you must use right shift.&lt;/p&gt;

&lt;p&gt;As for ctrl + ins, just use &lt;strong&gt;ctrl&lt;/strong&gt; + &lt;strong&gt;Fn&lt;/strong&gt; + &lt;strong&gt;E&lt;/strong&gt;, both ctrl keys work. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Get the host address for WSL2</title>
      <dc:creator>Li</dc:creator>
      <pubDate>Fri, 03 Mar 2023 08:54:27 +0000</pubDate>
      <link>https://dev.to/socrateslee/get-the-host-address-for-wsl2-2b2</link>
      <guid>https://dev.to/socrateslee/get-the-host-address-for-wsl2-2b2</guid>
      <description>&lt;p&gt;For WSL2, the ip address of a running distro is not static, neither the subset WSL2 uses is fixed. If you need to do something like accessing the services on the WSL2 host, you need to get the address by yourself.&lt;/p&gt;

&lt;p&gt;Get the ip address the current running distro:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ip -4 -br route get 8.8.8.8 | head -n 1|awk '{print $7}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Get the ip address the host(the gateway):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ip -4 -br route get 8.8.8.8 | head -n 1|awk '{print $3}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And get the netmask:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ifconfig eth0 |grep netmask|awk '{print $4}'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>opensource</category>
      <category>softwaredevelopment</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Convert String to CStr and back in rust</title>
      <dc:creator>Li</dc:creator>
      <pubDate>Wed, 10 Aug 2022 09:31:09 +0000</pubDate>
      <link>https://dev.to/socrateslee/convert-string-to-cstr-and-back-in-rust-1617</link>
      <guid>https://dev.to/socrateslee/convert-string-to-cstr-and-back-in-rust-1617</guid>
      <description>&lt;p&gt;Just some code snippets to convert rust &lt;code&gt;String&lt;/code&gt; to &lt;code&gt;std::ffi::CStr&lt;/code&gt;/&lt;code&gt;std::ffi::CString&lt;/code&gt; and back.&lt;/p&gt;

&lt;h2&gt;
  
  
  String to CString/CStr
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use std::ffi::CStr;
use std::ffi::CString;

fn main() {
   let s = "Hello World!".to_string();
   let c_string: CString = CString::new(s.as_str()).unwrap();
   let c_str: &amp;amp;CStr = c_string.as_c_str();
   println!("{:?}", c_str);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  CStr to String
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use std::ffi::CStr;

fn main() {
   let c_str: &amp;amp;CStr = CStr::from_bytes_with_nul(b"Hello World!\0").unwrap();
   let s: String = c_str.to_string_lossy().into_owned();
   println!("{:?}", s);
}
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
    </item>
    <item>
      <title>Sending real-time server logs to remote server with nc</title>
      <dc:creator>Li</dc:creator>
      <pubDate>Thu, 07 Jan 2021 06:15:32 +0000</pubDate>
      <link>https://dev.to/socrateslee/sending-real-time-server-logs-to-remote-server-with-nconly-4309</link>
      <guid>https://dev.to/socrateslee/sending-real-time-server-logs-to-remote-server-with-nconly-4309</guid>
      <description>&lt;p&gt;Remote debugging is hard, especially when you can't ssh into remote server, monitor the log generating. Maybe it is the server inside an office, or the security policy of your client doesn't allow remote login. But still, we can use nc to deliver the logs, real-timely. No complex setup required, just a little more than the beloved &lt;code&gt;tail -f&lt;/code&gt; command.&lt;/p&gt;

&lt;p&gt;nc(netcat) should be available in most the servers, so additional software are not required(and maybe your client doesn't have the permissions to install new software). &lt;/p&gt;

&lt;p&gt;Only two steps, first, on your own server, you may setup a server to listen for connections:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;nc -lvk &amp;lt;PORT_NUMBER&amp;gt; | tee remote.log
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, just let your client use the nc to sync the logs to your server, and now you get logs.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tail -f server.log | nc &amp;lt;SERVER_IP&amp;gt; &amp;lt;PORT_NUMBER&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Actually, even there is no nc at your client's server. You may actually send the by the following command&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;tail -f server.log &amp;gt; /dev/tcp/&amp;lt;SERVER_IP&amp;gt;/&amp;lt;PORT_NUMBER&amp;gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And remember to close the nc service when are done.&lt;/p&gt;

</description>
      <category>debug</category>
      <category>netcat</category>
      <category>nc</category>
    </item>
    <item>
      <title>About DoesNotExist Exception with Django ForeignKey</title>
      <dc:creator>Li</dc:creator>
      <pubDate>Wed, 16 Sep 2020 04:50:05 +0000</pubDate>
      <link>https://dev.to/socrateslee/about-doesnotexist-exception-with-django-foreignkey-coo</link>
      <guid>https://dev.to/socrateslee/about-doesnotexist-exception-with-django-foreignkey-coo</guid>
      <description>&lt;p&gt;For a model defined in Django，consider, a foreign key field pointed to User:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;class Article(Model):
    title = CharField()
    content = TextField()
    user = ForeignKey(to=get_user_model())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If &lt;code&gt;article&lt;/code&gt; is an instance of Article, &lt;code&gt;article.user&lt;/code&gt; would get a User instance(base on get_user_model()) based on the foreign key defined.&lt;/p&gt;

&lt;p&gt;If you happen to a project where the data consistency is not well maintained, e.g., the admin had deleted several old users despite the data dependency. When calling &lt;code&gt;article.user&lt;/code&gt;, you may get a DoesNotExist error, mostly look like&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DoesNotExist: User matching query does not exist. 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;From the source of Django, I found a comment in db/models/fields/related_descriptors.py:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# Assuming the database enforces foreign keys, this won't fail.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This means when dealing with the consistency of ForeignKey, Django tends to simply raise an error and leave it to the database admin. But in some cases, if we can resolve &lt;code&gt;article.user&lt;/code&gt; as a None object, it would be easier for the code to handle, or more compatible with other serializers/validators.&lt;/p&gt;

&lt;p&gt;I monkey-patched the module with the following code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from django.db.models.fields.related_descriptors import ForwardManyToOneDescriptor

def get_object(self, instance):
    qs = self.get_queryset(instance=instance)
    # Assuming the database enforces foreign keys, this won't fail.
    return qs.filter(self.field.get_reverse_related_filter(instance)).first()

ForwardManyToOneDescriptor.get_object = get_object
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The code replaces &lt;code&gt;query_set.get&lt;/code&gt; with &lt;code&gt;query_set.first&lt;/code&gt;, no exceptions raised but return a None when object does not exist. By the way, when if the uniqueness of the foreign key object can be assured, e.g. the foreign key is the primary key in the table related, &lt;code&gt;query_set.first&lt;/code&gt; is faster than &lt;code&gt;query_set.get&lt;/code&gt;, since it stops scanning the table on the first row matched.&lt;/p&gt;

</description>
      <category>python</category>
      <category>django</category>
    </item>
  </channel>
</rss>
